invalid use of incomplete type 'struct a'

This is a discussion on invalid use of incomplete type 'struct a' within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by ajoqiqu Do you understand ? Not exactly. I was hoping for a more concrete example. Some people ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by ajoqiqu
    Do you understand ?
    Not exactly. I was hoping for a more concrete example. Some people like to use things like animals, other people might talk about cars, but the point is to have something concrete to work with. It may be best if you provided the actual example of the things that you are trying to model.

    At the moment, I would be inclined to write:
    Code:
    class Base
    {
        // ...
    };
    
    class X
    {
        // ...
    private:
        Base* property1;
        Base* property2;
    };
    
    int main()
    {
        X object1;
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4
    Thank you, for your interest and your patience...

    I have some thing like this in my mind...

    Code:
    class object
    {
        int id;
        string name;
        property property_1;
    ///...
        property property_n;
    
        bool is_base_type;
        variant value;
    
    // if base_type is true : value is set
    // if base_type is false : the property are set
    
    
    };
    
    class property
    {
        int id;
        string name;
        int type_id;
        object type;
    
    };
    
    int main()
    {
    
    object my_house;
    my_object.id=1;
    my_object.name="house";
    my_object.is_base_type = 0;
    
    
    
    object roof;
    roof.id=2;
    roof.name="roof";
    roof.is_base_type = 1;
    roof.value = red;
    
    object key;
    key.id=3;
    key.name="key";
    key.is_base_type = 1;
    key.value = secret;
    
    
    
    property  a_roof;
    a_roof.id = 0;
    a_roof.name = "roof";
    a_roof.type_id = 2;
    a_roof.type = roof;
    
    
    property  a_key;
    a_key.id = 1;
    a_key.name = "key";
    a_key.type_id = 3;
    a_key.type = key;
    
    
    my_house.property_1 = a_roof;
    my_house.property_2 = a_key;
    
    }

    but my code refuse to compile... !
    Is it more clear with these few line ?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by ajoqiqu
    I have some thing like this in my mind...
    You should also describe your idea in words.

    Quote Originally Posted by ajoqiqu
    but my code refuse to compile... !
    I note that variant and string are not defined. You use the property class before it is defined. Furthermore, the object class has members of type property, but the property class has members of type object. This is akin to you placing a bag A into a bag B, and then placing B into A, without removing A from B.

    I suggest that you take some time to learn about the use of inheritance and polymorphism, e.g., from a book. Accelerated C++ by Koenig and Moo could give you a better foundation in the basics of C++, including this area.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ajoqiqu View Post
    but my code refuse to compile... !
    Is it more clear with these few line ?
    You need to take a few steps back. Now, don't be offended: There are some rather glaringly obvious reasons that code will not compile, such as the issue of "namespaces" (you aren't using any).

    This kind of indicates this is one of the first programs you have ever written in C++. That being the case, you should not write 50-100 lines of code and then try to compile it. That is a HUGE mistake. You should write maybe 5-10 lines of code, and then compile it, even if the code does not do anything, to make sure you do not just repeat the same error over and over again. So, starting from scratch, let's just try and compile this part -- notice I added the necessary includes and namespace directive:
    Code:
    #include <string>
    
    using namespace std;
    
    class property
    {
        int id;
        string name;
        int type_id;
        object type;
    
    };
    
    class object
    {
        int id;
        string name;
        property property_1;
    ///...
        property property_n;
    
        bool is_base_type;
        variant value;
    
    // if base_type is true : value is set
    // if base_type is false : the property are set
    
    
    };
    
    int main()
    {
           /* NOTHING HERE YET */
    	return 0;
    }
    Also, I swapped the order of the class definitions around so that object can include property, but then of course we are still left with these errors:

    test.cpp:10: error: ‘object’ does not name a type
    test.cpp:23: error: ‘variant’ does not name a type


    Because of course there's an object in property. This can be solved by forward declaring "object" at the top:
    Code:
    class object;
    HOWEVER, this is an incomplete type, so the "object" in property must be a pointer instead of a real instance:
    Code:
       object *type;
    Now, we are only left with one error:

    test.cpp:24: error: ‘variant’ does not name a type

    Sort of mysterious -- this is why you should always be test compiling your code, and not treating it as a sort of canvas for ideas. Ideas can compile, but they do not necessarily have to!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #20
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    That being the case, you should not write 50-100 lines of code and then try to compile it.
    This is good advice for later as well when you are trying to debug a huge code base. It is best to attempt to isolate the code or isolate the file where the code is (only compile the cpp instead of the project or solution) and then work backwards from there.

    You really need to read a bit more about C++ before attempting to do something as complex as this. I do admire your willingess to jump in and get your feet wet so I really cannot fault you for at least trying. Another equally important skill is learning what it is that your compiler is trying to tell you. I still get errors from time to time that leave me at my desk scratching my head as to what the compiler is attempting to tell me. If you cannot decipher what the compiler is telling you then you really cannot address the issue. It might not be a bad idea to google the error code or warning code until you become comfortable with your compiler. I learned from the help files but I don't recommend that b/c finding the error and warning codes in help is like looking for a needle in a haystack.
    Last edited by VirtualAce; 05-16-2010 at 01:02 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM

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