reference class object within a class

This doesn't seem to work, it gives an error "incomplete type not allowed":

1
2
3
4
5
6
7
8
class Joe
{
	int length;
        int value[MAX_LENGTH];
	Joe nextJoe;

}

I'm trying to point to another "Joe" when value reaches a max length.

Thanks,
-dog


class constructors are private by default. try placing the public keyword before int length.

You also have not defined a copy constructor. In order to do what you need, consider changing nextJoe into a Joe*. THen you will need to dynamically allocate memory for nextJoe.

Is MAX_LENGTH a #define? Where is this coming from ?
Change nextJoe to a Joe*
Constructors are not private by default. Member functions of a class are public by default. Only data members of a class are private by default. Yes, you could use a pointer to point to the next object like in a linked list. However, if you make those data members public you destroy any reason for using a class in this instance and might as well just use a struct. If you still want to use a class your best bet is to keep the data members private and add the member functions needed to manipulate your data (including a function to set the pointer to the next object when a new one is created.)
Constructors are not private by default. Member functions of a class are public by default.


Uh... no? Implicitly declared default constructors and destructors are public, everything else is private unless otherwise specified
Doggonit, you are right Maer. I forgot the difference between the implicit and explicit constructor/destructor membership. Thanks for clearing that up. :)
To go back to the original question - the definition of Joe is incomplete until the end of the class. You can't have another member of type Joe within Joe, because the compiler can't know what a "Joe" is until you've finished defining it. Plus, if every Joe contained another Joe, then it would be infinitely big.

You can, however, point to an incomplete type. You can therefore use a pointer to a Joe inside your definition of Joe.

Whoever Joe is, his ears must be burning by now...
Last edited on
Topic archived. No new replies allowed.