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?Quote:
Originally Posted by Dev-C++
Printable View
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?Quote:
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 -
Quote:
error C2460: 'my_class::ply' : uses 'my_class', which is being defined
Thanks, that worked.
But why a pointer to work?
You can't do something like this.Quote:
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.
>> 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.
I see.
BTW: twomers, you posted late.
You're right, but I got distracted by something shinyQuote:
Originally Posted by Cool-August
Your sig?Quote:
Originally Posted by twomers
You're just jealous. But yes :rolleyes:Quote:
Originally 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 :rolleyes: Site's been updated to use MySQL, isn't that exciting?
Why don't you say your upgrading to MySQL on the website?Quote:
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 ).