include

This is a discussion on include within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm building a program with 4 classes: 1) Abtract-Base class - Father 2) Inherit Class - A 3) Inherit ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    15

    Unhappy include

    Hello.

    I'm building a program with 4 classes:
    1) Abtract-Base class - Father
    2) Inherit Class - A
    3) Inherit Class - B
    4) Inherit Class - C

    I need A, B and C to include each other headers, but I seem to have some problem, probebly with has to do with all 3 classes including each other (tought I have #ifndef and #define in each header).

    Solution anyone? I'm quite helpless...

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If the classes only need to know that the name of the siblings exist - only use references/pointers to refer to those - then those headers are not included in headers and forward declarations are used instead.
    Code:
    //a.h
    ...
    class B;
    class A
    {
        ...
        void foo(const B& b);
    };
    
    //a.cpp
    #include "a.h"
    #include "b.h" //for full type
    void A::foo(const B& b) {...}

    If they actually want to declare instances of those, you are probably screwed.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    Quote Originally Posted by anon View Post
    If they actually want to declare instances of those, you are probably screwed.
    That means you have to rethink your design
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    I'm almost there...

    Say I don't have any .cpp files. All implementation are in the headers.
    Now in header B I have an object name some_obj typed A*, and I want to call
    some_obj->print().
    How do i make the call syntaxly? A::some_obj->print () doesn't work...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    As you normally would,
    some_obj->print();
    Assuming you've included the header and the compiler can see the object whose member function you want to call.
    Unless it's inside a namespace (or is a static member of a class) named A, which would make your above syntax correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    It doesn't work. for some reason the compailer claims "use of undefined type 'A'" when I tyr to call some_obj->print();


    I'm soon to be out of my mind... anyone????

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    You haven't included the appropriate header for the definition of the class or type A (or you have circular references [A including B and B including A]).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have to use cpp files, at least for functions that use the forward declared names. The thing is that the declarations must not know the full type of other classes (in this case - because this would create circular dependencies) but the implementations do.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    The thing is i'm implementing an operator so I can't do it in a .cpp file, can I???

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,902
    Unless you are using templates, you can.
    Even if you are using templates, however, you can still split the definition and the implementation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    For multiple dispatch you only need forward declarations. You shouldn't need anything more to implement the problem from your other thread.

    The reason is that the operators you are defining should take references to const, and a reference requires only a forward declaration.

    Then, after all your classes are defined, you can implement the operator functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debug assertion failed!
    By chintugavali in forum C Programming
    Replies: 4
    Last Post: 12-11-2007, 06:23 AM
  2. Socket programming
    By kahad in forum C Programming
    Replies: 3
    Last Post: 12-14-2006, 04:37 PM
  3. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  4. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM
  5. MFC Assertion Failure
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 08-01-2002, 10:58 AM

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