LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-18-2011, 01:34 PM   #1
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Rep: Reputation: Disabled
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.
 
Old 06-18-2011, 02:12 PM   #2
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Quote:
Originally Posted by just1alexx View Post
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)'.

Lyle.
 
Old 06-18-2011, 02:20 PM   #3
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-18-2011, 02:43 PM   #4
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
Sorry, I was wondering after typing that all up if I was answering your question, but I left it thinking you'd tell me if I wasn't.

I'm a little confused. Your code compiles at http://www.comeaucomputing.com/tryitout/ without error.

'PRE' isn't a type; it's a value of type 'mode', so g++ 4.4.5 is looking for a type there. Sorry, not sure what's going on.

Lyle.
 
Old 06-18-2011, 02:45 PM   #5
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Original Poster
Rep: Reputation: Disabled
No problem, I'll find a workaround, thanks for the help
 
Old 06-18-2011, 02:46 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by just1alexx View Post
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.
 
Old 06-18-2011, 02:56 PM   #7
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-18-2011, 03:08 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by just1alexx View Post
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.
 
Old 06-18-2011, 03:19 PM   #9
just1alexx
LQ Newbie
 
Registered: Jun 2011
Location: Cranford, NJ
Distribution: don't have a preference (ubuntu right now)
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-18-2011, 03:24 PM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Yes, the correct usage of terms sometimes escapes me too.

function prototype = function declaration

function definition = function implementation
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++ : Constructor in 'Private' section of class thelink123 Programming 8 08-30-2010 12:48 AM
C++, How to initialize nested class constructor?? jayasekar Programming 4 04-02-2010 12:18 PM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM
Class constructor not being called ChimpFace9000 Programming 1 06-03-2002 08:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration