including headers mutually?

This is a discussion on including headers mutually? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have to classes CVImage.hpp and MyLabel.hpp In MyLabel.hpp I have a #include "CVImage.hpp" and now I want to ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question including headers mutually?

    Hi,
    I have to classes
    CVImage.hpp and MyLabel.hpp
    In MyLabel.hpp I have a #include "CVImage.hpp"
    and now I want to use MyLabel from CVImage to I added #include "MyLabel.hpp" in CVImage.hpp

    And I got like 83 errors!
    So I wonder if including headers mutually like this is the main reason of this errors

    Thanks
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Yes, you cannot include MyLabel.hpp in CVImage.hpp if CVImage.hpp includes MyLabel.hpp. However, you can include MyLabel.hpp in the relevant source file that implements the functions declared in CVImage.hpp. If these declarations require CVImage to be declared, you can just use a forward declaration.
    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

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you headers can't include each other "mutually". The include directive really just "pastes" the header into the source and it is inevitable that one header will be before another and will not be able to see the declarations in the other header that end up coming afterwards in the source.

    Search for forward declarations.
    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).

  4. #4
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    thanks but

    Ok, thanks. (As I thought)
    but... how do I write a forward declaration in c++ ?

    thanks
    I just found it

    class MyLabel;
    Mac OS 10.6 Snow Leopard : Darwin

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by nacho4d
    how do I write a forward declaration in c++ ?
    If you searched you would have found examples

    Most likely:
    Code:
    class CVImage;
    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

  6. #6
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question

    adding
    class MyLabel;
    to CVImage.hpp works fine In the meaning that It compiles.

    but when start using it, it does not compile anymore
    for example inside some CVImage method:
    MyLabel labelProcessor;
    labelProcessor.Labeling(something);

    I got errors like :
    error C2079: 'labelProcessor' uses undefined class 'MyLabel'
    error C2228: left of '.labeling' must have class/struct/union

    I wonder what am I missing here. Is like "class MyLabel;" is not having effect at all.

    I have noticed that In most examples the use forward declaration to declare instance variables in a class. But in my case is different, I just want to use it inside the implementation.
    Also I wonder if that has to be something with using hpp files where the interface and implementation are in the same file.

    Thanks
    Mac OS 10.6 Snow Leopard : Darwin

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by nacho4d
    I wonder what am I missing here. Is like "class MyLabel;" is not having effect at all.
    The forward declaration declares the class, but does not define it. To actually use an object of the class, its definition must be available. This usually entails including the relevant header that contains this class definition.

    Quote Originally Posted by nacho4d
    I have noticed that In most examples the use forward declaration to declare instance variables in a class.
    You probably saw the use of a forward declaration preceding the declaration of a pointer member variable. A forward declaration of X would be insufficient if you want to define a class with a member of type X.

    Quote Originally Posted by nacho4d
    But in my case is different, I just want to use it inside the implementation.
    Also I wonder if that has to be something with using hpp files where the interface and implementation are in the same file.
    You would have to separate the implementation of the functions into a source file.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. including headers and forward declaration
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2007, 02:47 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. problems with including headers
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 08:06 AM
  5. including headers inside headers
    By kromozom in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2005, 10:56 AM

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