Thread: Why isn't my class working?

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Why isn't my class working?

    When I try to compile this:
    Code:
    class my_class;
    class my_class
    {
       my_class ply;
       char board[10];
    };
    Dev-C++ returns the error:
    Quote Originally Posted by Dev-C++
    field `ply' has incomplete type
    Why? And how can I fix it?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    class my_class;
    class my_class
    {
       my_class *ply;
       char board[10];
    };
    ? I think it needs to be of pointer type.

    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.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thanks, that worked.

    But why a pointer to work?

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Cool-August
    When I try to compile this:
    Code:
    class my_class;
    class my_class
    {
       my_class ply;
       char board[10];
    };
    Dev-C++ returns the error:

    Why? And how can I fix it?
    You can't do something like this.

    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.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> 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.

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I see.

    BTW: twomers, you posted late.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by Cool-August
    BTW: twomers, you posted late.
    You're right, but I got distracted by something shiny

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by twomers
    You're right, but I got distracted by something shiny
    Your sig?

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by Cool-August
    Your sig?
    You're just jealous. But yes

  10. #10
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> You're just jealous. But yes

    Why would I be jealous of "Site down for exciting upgrades!"?

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I meant the colors! Honestly Site's been updated to use MySQL, isn't that exciting?

  12. #12
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by twomers
    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?

    And my sigs got colors ... kind'a ... sort'a ... see?

  13. #13
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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 ).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  2. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. working with a class function
    By Noobie in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2003, 07:07 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM