When I try to compile this:
Dev-C++ returns the error:Code:class my_class; class my_class { my_class ply; char board[10]; };
Why? And how can I fix it?Originally Posted by Dev-C++
When I try to compile this:
Dev-C++ returns the error:Code:class my_class; class my_class { my_class ply; char board[10]; };
Why? And how can I fix it?Originally Posted by Dev-C++
? I think it needs to be of pointer type.Code:class my_class; class my_class { my_class *ply; char board[10]; };
If you're interested I get this error on visual studio -
error C2460: 'my_class::ply' : uses 'my_class', which is being defined
Last edited by twomers; 09-17-2006 at 10:48 AM.
You can't do something like this.Originally Posted by Cool-August
Think about it this way:
sizeof(my_class) = sizeof(ply) + sizeof(board);
sizeof(my_class) = sizeof(my_class) + sizeof(char[10]);
You need to make ply a pointer (or a reference but that would be very difficult to use); essentially my_class objects would have infinite size in your original code because they'd be 10 characters larger than themselves.
Imagine trying to make a semi truck+trailer, whose trailer was large enough to hold an identical semi truck+trailer plus some additional cargo. Impossible, no?
You would get into even more craziness than that, though -- for example, when you construct a my_class object, it would automatically construct another my_class object for ply, which would construct another my_class object, which would keep going on infinitely.
Last edited by Cat; 09-17-2006 at 10:59 AM.
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
>> But why a pointer to work?
I won't be able to explain (coherently), nearly as good as someone else, so I'll leave it to them.
You're right, but I got distracted by something shinyOriginally Posted by Cool-August
You're just jealous. But yesOriginally Posted by Cool-August
>> You're just jealous. But yes
Why would I be jealous of "Site down for exciting upgrades!"?
I meant the colors! Honestly Site's been updated to use MySQL, isn't that exciting?
Why don't you say your upgrading to MySQL on the website?Originally Posted by twomers
And my sigs got colors ... kind'a ... sort'a ... see?
You can use a pointer because the size of a pointer is always known at compile time. You also have to call new on the pointer to call the constructor. Creating an object within itself would cause the constructor to recurse into itself infinitely ( as Cat pointed out ).