Danikaze
Danikaze

Reputation: 193

C++ Compiler tells me a type is not recognized

Using Visual C++ 2010 I have a code like the following:

file A.hpp:

...
#include "R.hpp"
...
class A; // forward declaration because the APtr_t is used inside the A class too.
typedef boost::shared_ptr<A> APtr_t;
...
class A {
...
    void some_method () {
       ...
       R::get()->mehod(); // singleton ;)
       ...
    }
...
};

file R.hpp:

...
#include "A.hpp"
...

class R {
...
APtr_t method();
...
};

The Visual C++ editor says it's fine (no error marked) but when compiling the project, it acts as APtr_t was not defined. It shows errors like this:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

The thing is that this problem only is happening in the R.hpp file, I think...

Do you have any idea? It's pretty confusing :-/

Thanks in advance.

Upvotes: 0

Views: 1613

Answers (2)

Shuo
Shuo

Reputation: 8937

I believe this is a header files include loop, which means A include B, B includes A. Normally this will introduce such issues. In your cpp files, no matter you include which first, in whatever sequence, it always reports such problems. The solution is don't use loop include.

I guess you don't have any cpp files. Then maybe you can introduce another hpp file, type.hpp, which purely defines the class interface but no implementation, then in A.hpp and R.hpp, you can write your member function code.

type.hpp

class A; // forward declaration because the APtr_t is used inside the A class too.
typedef boost::shared_ptr<A> APtr_t;
...
class A {
...
void some_method ();
...
};

class R {
...
APtr_t method();
...
};


a.hpp 
#include "type.hpp"
void A::some_method () {
   ...
   R::get()->mehod(); // singleton ;)
   ...
}


r.hpp
#include "type.hpp"
....    

Upvotes: 0

Mark B
Mark B

Reputation: 96241

My psychic debugging skills guess that A.hpp includes R.hpp and that your headers have proper include guards. In this case the include chain would look like blah.cpp -> A.hpp -> R.hpp -> A.hpp (include guard prevents inclusion). So it never saw A.hpp's contents inside R.hpp at all. You'll need to use one of the standard methods for removing circular dependencies.

Upvotes: 4

Related Questions