classes as member variables

This is a discussion on classes as member variables within the C++ Programming forums, part of the General Programming Boards category; Hi, I don't have a lot of time atm, so I'll save you the usual verbose intro of how I ...

  1. #1
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118

    classes as member variables

    Hi,

    I don't have a lot of time atm, so I'll save you the usual verbose intro of how I got myself in my current predicament :P

    Basically I have a header file, looks something like this

    Code:
    #ifndef CLASSES_H
    #define CLASSES_H
    
    #include <iostream>
    using namespace std;
    
    
    //******* Part **************
    
    //Abstract class of parts
    class Part
    {
    	public:
    		Part():itsPartNumber(1){}
    		Part(int num):itsPartNumber(num){}
    		virtual ~Part(){};
    
    		int getPartNumber(){ return itsPartNumber; }
    		virtual void Display() const = 0; //must be overidden
    
    	private:
    		int itsPartNumber;
    };
    
    //some other derived classes, ommited to save space
    
    //********** Part Node *************
    
    class PartNode
    {
    	public:
    		PartNode(*Part);
    		~PartNode();
    
    		void setNext(PartNode * node){ itsNext = node; }
    
    		PartNode * getNext() const;
    		Part* getPart() const;
    
    	private:
    		Part* itsPart;
    		PartNode * itsNext;
    
    };
    
    #endif
    I have another .cpp file which "includes" this header and simply implements the pure virtual function in the abstract class. I also have a main.cpp file which contains the driver (main) function.

    When I try to compile I get the following.

    Code:
    error C2327: 'PartNode::Part' : is not a type name, static, or enumerator
    I get it a couple of times, once for the function which returns a pointer to a Part, and the other time when I declare one of the member variables of the node class to be a pointer to a Part.

    They are the only errors I'm getting.

    I think I'm making some fundamental error when it comes to having classes as member variables of other classes, but I don't know what it is. Any help would be much appreciated, thanks

    Regards,

    Stonehambey

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    Perhaps PartNode(*Part) should be: PartNode(Part*)

    Oh yes, and do not use using directives in header files except within some restricted scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118
    Quote Originally Posted by laserlight View Post
    Perhaps PartNode(*Part) should be: PartNode(Part*)

    Oh yes, and do not use using directives in header files except within some restricted scope.
    Interesting, that solves the problem...but I don't know why

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    *Part dereferences some pointer named Part, but Part is a class name, not a pointer. Part* is the type of a pointer to a Part object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118
    ok, I see my error there.

    Why was the compiler specifically flagging these two lines though?

    Code:
    Part* getPart() const;
    
    ...
    
    Part* itsPart;
    When the error was in the PartNode constructor.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    Why was the compiler specifically flagging these two lines though?
    Unfortunately, I do not know. Part seems to play the part of a type name in those two lines.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    66
    Maybe the fault is in the constuctor of PartNode ...
    Also you should define a standard constuctor for every class.


    Greetz

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, you should never put using statements inside a header file or before an #include statement; otherwise it could change the meaning of your code. i.e. Don't put this in your header files:
    Code:
    using namespace std;

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Also you should define a standard constuctor for every class.
    I disagree with this advice. You should only define a standard constructor when you need it, and often you don't. If you leave it out you help enforce proper use of your class.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    What is a standard constructor? A default constructor?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> What is a standard constructor? A default constructor?
    I don't know, that's what I assumed and what I was commenting on.

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    66
    Quote Originally Posted by laserlight View Post
    What is a standard constructor? A default constructor?
    Yes, I ment the default constructor, sorry, I'm german ...

    EDIT:
    I disagree with this advice. You should only define a standard constructor when you need it, and often you don't. If you leave it out you help enforce proper use of your class.
    But related to the code above, this declaration will produce an error
    Code:
    PartNode pn1;    // Error
    , doesn't it ?


    Greetz
    Last edited by Greenhorn__; 08-14-2008 at 03:49 PM.

  13. #13
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118
    Hi, thanks for the replies everyone

    I compile as I go along, which I have found allows me to catch and locate errors quicker. In this case the error occurred before I had time to define the default constructor for the PartNode class, (which, ironically, was the source of the error :P)

    Sorry about using namespace std. This program was to play a bit with abstract classes and virtual member functions, which I've just learned about, so I was feeling a bit lazy, although I realise that's no excuse to pick up bad habits! :P

    Ciao

    Stonehambey

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> But related to the code above, this declaration will produce an error
    >> PartNode pn1; // Error

    Yes, it would, but I don't see that code anywhere. In fact, one reason to not specify the default constructor is so that the code would produce an error because that's not how you're supposed to use that class.

    Whether this particular class needs a default constructor or not I do not know, but in general you should only add one if it is necessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Storing classes in variables?
    By jw232 in forum C++ Programming
    Replies: 10
    Last Post: 02-19-2009, 06:34 PM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 11:59 PM
  4. Replies: 1
    Last Post: 04-17-2008, 08:45 PM
  5. Derived member variables in initializer lists
    By bennyandthejets in forum C++ Programming
    Replies: 13
    Last Post: 11-27-2003, 04:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21