Thread: including headers mutually?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05: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