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

02-21-2010

laserlight

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.

02-21-2010

anon

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.

02-21-2010

nacho4d

thanks but

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

thanks
I just found it

class MyLabel;

02-21-2010

laserlight

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;

02-21-2010

nacho4d

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

02-21-2010

laserlight

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.