C ++编译器告诉我一个类型无法识别

时间:2011-02-01 22:12:22

标签: c++ boost

使用Visual C ++ 2010我有如下代码:

文件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 ;)
       ...
    }
...
};

文件R.hpp:

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

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

Visual C ++编辑器说它很好(没有标记错误)但是在编译项目时,它的作用是APtr_t没有定义。 它显示如下错误:

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

问题是这个问题只发生在R.hpp文件中,我想......

你知道吗? 这很令人困惑: - /

提前致谢。

2 个答案:

答案 0 :(得分:4)

我的通灵调试技能猜测A.hpp包括R.hpp,并且您的标头有正确的包含警卫。在这种情况下,包含链看起来像blah.cpp - &gt; A.hpp - &gt; R.hpp - &gt; A.hpp (include guard prevents inclusion)。因此,它根本没有在A.hpp内看到R.hpp的内容。您需要使用其中一种标准方法来删除循环依赖项。

答案 1 :(得分:0)

我相信这是一个头文件包含循环,这意味着A包括B,B包括A. 通常这会引入这样的问题。在你的cpp文件中,无论你先包含哪个,无论顺序如何,它总是会报告这些问题。 解决方案是不要使用循环包含。

我猜你没有任何cpp文件。那么也许你可以引入另一个hpp文件,type.hpp,它纯粹定义了类接口但没有实现,然后在A.hpp和R.hpp中,你可以编写你的成员函数代码。

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"
....    
相关问题