Can't find my constructor

This is a discussion on Can't find my constructor within the C++ Programming forums, part of the General Programming Boards category; I'm trying to compile a program I made, however, I keep getting an error form MSVC++ that one of my ...

  1. #1
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34

    Can't find my constructor

    I'm trying to compile a program I made, however, I keep getting an error form MSVC++ that one of my classes has no constructors. (Not that there is no appropriate constructor, it's telling me I don't even have one).

    Here's the definition for the class (it's part of a linked list):
    Code:
    class InvTail : public InvItem
    {
    public:
    	InvTail(InvItem *);
    	virtual ~InvTail() {}
    	...
    private:
    };
    
    // prev and next are inherited from InvItem
    InvTail::InvTail(InvItem *head)
    {
    	prev = head;
    	next = 0;
    }
    The only place the InvTail constructor is called from is the InvHead constructor. (The head creates the tail, you know the drill). All the constructor does is try and create a new InvTail on the heap and pass it's this pointer as the argument.

    Code:
    InvHead::InvHead()
    {
    	next = new InvTail(this);
    }
    If I change "next = new InvTail(this);" to "next = new InvTail();" it tells me it can't find an appropriate constructor, (as I would expect), but as it is it gives simply tells me that there isn't a constructor, and the compile fails. Any help would be appreciated.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You probably called the constructor before you defined your class.

    IE you probably did this:

    class InvTail;

    // Some stuff here

    InvHead::InvHead()
    {
    next = new InvTail(this);
    }

    // Then after that you did

    class InvTail : public InvItem
    {
    // blah blah blah
    };
    Last edited by Polymorphic OOP; 12-06-2002 at 06:55 PM.

  3. #3
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Ok, heres some more code:

    Code:
    class InvItem;	// linked list superclass
    class InvHead;	// head
    class InvTail;	// tail
    
    // INVITEM
    class InvItem
    {
    public:
    	InvItem();
    	virtual ~InvItem() { delete next; }	// don't delete prev because prev calls the deconstructer while deconstructing itself
    
    	InvItem *getNext() { return next; }
    	InvItem *getPrev() { return prev; }
    protected:
    	InvItem *next, *prev;
    };
    
    InvItem::InvItem():
    prev(0),
    next(0)
    {}
    
    // INVHEAD
    class InvHead : public InvItem
    {
    public:
    	InvHead();
    	virtual ~InvHead() {}	// InvItem deconstructor cleans up
    private:
    };
    
    InvHead::InvHead()
    {
    	next = new InvTail(this);
    }
    
    // INVTAIL
    class InvTail : public InvItem
    {
    public:
    	InvTail(InvItem *);
    	virtual ~InvTail() {}
    private:
    };
    
    InvTail::InvTail(InvItem *head)
    {
    	prev = head;
    }
    That's the code for the head and tail with just their constructors + deconstructors included. I still get the error though, so I think that should be enough. (At this point this is most of the code anyways).

  4. #4
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    Originally posted by Polymorphic OOP
    You probably called the constructor before you defined your class.
    Yeah, that's it. I assumed prototyping the class at the beginning of the file would let me do that. Sense that doesn't work, is there any way I can write it so that I CAN write my classes/member functions in an arbitrary order?

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You always have to have a declaration of a function before you call it whether it be a constructor of some other member function -- just like when you work with nonmember functions.

    So you can define your member functions in any order you want, just make sure you have declarations for the functions before you call them (IE define the class first).

    As long as you define all of your methods in cpp's and you #include the definitions of the classes in that file, you should be fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM
  3. Call constructor within constructor. How to?
    By xErath in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 04:30 PM
  4. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM
  5. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07:28 AM

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