• AVR Freaks

AnsweredHot!Using dot to reference an enum value

Author
wysiwyg
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2010/07/27 09:25:10
  • Location: 0
  • Status: offline
2019/08/29 12:42:10 (permalink)
0

Using dot to reference an enum value

I'm using MPLab X v5.15 and XC16 1.36
I have built some structures for variables and throughout my program, I'm use the dot to reference various elements.
  i.e. int32Number =  nPage.Cycler.Object.CycleCounter.Value
The details of my example above are not important. It's just a structure with some elements such as named ints and strings.
 
I realize that the dot is supposed to select an element of a structure.  However, I've discovered that if I type the name of an enum and then type dot, the editor shows me a list of the named values and I can select one.  However, at compile time, there is an error "  expected expression before 'enum name'  ".
 
I know that an enum doesn't have elements per se, so how could the dot select anything.  An enum is a named list of integers.  Without a typedef, it behaves more like a list of defines except that the values are limited to int.  However, the MPLab X editor does allow you to type such a syntax as if it understands what you are trying to do, so it has left me wondering why the compiler chokes on it.
 
Just wondering if there's a way to use the dot syntax with an enum and have it work.
 
i.e.
typedef enum __attribute__ ((packed)) _COLORS {
 BLACK = 0,
 BLUE = 31,
 BROWN = 48192,
 GREEN = 2016,
 YELLOW = 65504,
 RED = 63488,
 GRAY = 33840,
 WHITE = 65535
} COLORS;

COLORS myColor;

// this fails to compile...
myColor = COLORS.WHITE;

// but this works fine...
myColor = WHITE;

#1
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Using dot to reference an enum value 2019/08/29 13:18:34 (permalink) ☄ Helpfulby wysiwyg 2019/08/30 14:21:43
+1 (1)
I doubt it.
It is the NetBeans editor doing that, possibly because some C++ compiler has support for it.
If the XC16 Compiler manual doesn't mention it, then it's not possible, despite any "hints" NetBeans may confusingly give.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
andersm
Super Member
  • Total Posts : 2675
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: Using dot to reference an enum value 2019/08/29 14:49:20 (permalink) ☼ Best Answerby wysiwyg 2019/08/30 14:21:54
+1 (1)
Not in C. In C++ you can, if the enum is a class member and unscoped (not a C++11 "class enum"):
struct X {
    enum e {
        a = 1,
    };
};
 
X x;
X* px = &x;

int i = x.a; // OK
int j = X::a; // OK
int k = px->a; // OK
int l = X.a; // does not compile

#3
wysiwyg
New Member
  • Total Posts : 11
  • Reward points : 0
  • Joined: 2010/07/27 09:25:10
  • Location: 0
  • Status: offline
Re: Using dot to reference an enum value 2019/08/30 14:26:35 (permalink)
0
I sure do appreciate the help from everyone.  It saved me from wasting time mucking around with something that stood no chance of working.  It would have been nice to be able to use enums like this during dev.  At least it works for structured vars which is super helpful.
 
Although, if it can work in C++ then maybe it'll be useful to me in future such projects so the example is still very much appreciated.
 
Thanks,
Bill B.
#4
Jump to:
© 2019 APG vNext Commercial Version 4.5