[SOLVED] Passing an enum value to a nested class constructor C++
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: don't have a preference (ubuntu right now)
Posts: 12
Rep:
Passing an enum value to a nested class constructor C++
Hi, the compiler (g++ 4.4.5) does not recognize PRE as a type in the following code:
Code:
enum mode {PRE, IN, POST};
class tree
{
public:
class iter
{
public:
iter (mode m);
};
tree (mode m);
iter alex (PRE); /* PRE does not define a type */
};
Why?
Last edited by just1alexx; 06-18-2011 at 02:01 PM.
Hi, the compiler (g++ 4.4.5) does not recognize PRE as a type in the following code:
Code:
enum mode {PRE, IN, POST};
class tree
{
public:
class iter
{
public:
iter (mode m);
};
tree (mode m);
iter alex (PRE); /* PRE does not define a type */
};
Why?
Your code compiles:
Code:
lyle@bowman:~$ g++ --version
g++ (GCC) 4.4.4
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
lyle@bowman:~$ g++ -c test.cxx
but doesn't link:
Code:
lyle@bowman:~$ g++ test.o
test.o: In function `main':
test.cxx:(.text+0x15): undefined reference to `tree::tree(mode)'
test.cxx:(.text+0x26): undefined reference to `tree::iter::iter(mode)'
collect2: ld returned 1 exit status
because you haven't provided definitions for 'tree::tree(mode)' and 'tree::iter::iter(mode)'; you have only provided declarations for these things.
In summary, the compiler does recognize 'PRE' as a value of type 'enum mode', so it does compile. It won't link because you haven't provided definitions/implementations of 'tree::tree(mode)' and 'tree::iter::iter(mode)'.
Distribution: don't have a preference (ubuntu right now)
Posts: 12
Original Poster
Rep:
Not in mine:
Code:
alex@sabertooth: /mnt/data/Programming$ g++ --version
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Code:
alex@sabertooth: /mnt/data/Programming$ g++ -c test.cpp
test.cpp:16: error: ‘PRE’ is not a type
alex@sabertooth: /mnt/data/Programming$
I know that I haven't provided any implementation yet, I was just compiling.
Last edited by just1alexx; 06-18-2011 at 02:22 PM.
Hi, the compiler (g++ 4.4.5) does not recognize PRE as a type in the following code:
Code:
...
iter alex (PRE); /* PRE does not define a type */
};
Why?
In the code above, I believe you are attempting to construct a variable named 'alex', with it's initial value set to PRE. However, to the compiler, what you are doing is declaring a function that accepts a PRE type, and returns an iter object. The data type PRE does not exist, and hence the error. Do *not* confuse data type with enum value.
Anyhow, the proper place to construct 'alex' is in the tree constructor.
Sorry for changing your variables names; I can't stand to see someone deviate from my coding standards.
Code:
enum Mode {PRE, IN, POST};
class Tree
{
public:
class Iter
{
public:
Iter(Mode mode);
};
Tree(Mode mode) : alex(PRE) {}
Iter alex;
};
// missing implementation for Iter constructor.
int main()
{
Tree tree(POST);
}
Last edited by dwhitney67; 06-18-2011 at 02:55 PM.
Distribution: don't have a preference (ubuntu right now)
Posts: 12
Original Poster
Rep:
Ohhh... I see, I did not realize that it could be also interpreted as a prototype.
But then, wouldn't every public object that I try to declare be interpreted as a prototype also?
Ohhh... I see, I did not realize that it could be also interpreted as a prototype.
But then, wouldn't every public object that I try to declare be interpreted as a prototype also?
I'm not sure I understand what you are stating above; perhaps you could provide an example. Earlier you were attempting to declare and initialize a variable within the class, using a style that resembles that of a function declaration. You confused the compiler into believing that you were declaring a function. It did not know what data type PRE is. PRE is not a type; it is an enum value.
If you tried something like this:
Code:
Iter alex = PRE;
then the compiler will also complain. You cannot initialize variable within the class declaration, unless you declare it static and you are assigning a numeric (an int) value to the variable. PRE is not numeric; it is an enum value, and hence even the code above would not work with the static attribute.
Last edited by dwhitney67; 06-18-2011 at 03:09 PM.
Distribution: don't have a preference (ubuntu right now)
Posts: 12
Original Poster
Rep:
nevermind, you just answered my question when edited your code. I see now, thanks a lot, now I know what to do.
Sorry for the prototype thing, is just another word that i use borrowed from C to say function declaration.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.