我正在尝试实现一个具有类成员指针的类和返回指针的方法但是在编译时我得到“语法错误:缺少';'在'*'“和”缺少类型说明符 - 假设的int之前。注意:C ++不支持default-int“错误
这是代码:
main.cpp中:
#include "AClass.h"
#include "BClass.h"
int main ( int argc, const char* argv[] )
{
AClass a;
BClass b;
return 0;
}
AClass.h:
#ifndef ACLASS_H
#define ACLASS_H
#include "BClass.h"
class AClass
{
public:
BClass* getB ();
void setB (BClass* inst);
private:
BClass* b;
};
#endif
BClass.h:
#ifndef BCLASS_H
#define BCLASS_H
#include "AClass.h"
class BClass
{
public:
AClass* getA ();
void setA (AClass* inst);
private:
AClass* a;
};
#endif
我甚至没有使用cpp文件充实类,我收到一串错误: 如果我创建C ++文件并定义所有内容并不重要,这些错误仍然存在。
1>------ Build started: Project: memberUDFpointers, Configuration: Debug Win32 ------
1> Main.cpp
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): error C2143: syntax error : missing ';' before '*'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): warning C4183: 'getA': missing return type; assumed to be a member function returning 'int'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(10): error C2061: syntax error : identifier 'AClass'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(12): error C2143: syntax error : missing ';' before '*'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我已经看了很多不同的帖子,但我仍然在这个问题上摸不着头脑。 有人能给我一些线索吗?
答案 0 :(得分:1)
您对AClass的定义取决于BClass的定义,而BClass的定义取决于AClass的定义。在另一个已定义之前,您无法定义一个。
幸运的是,实际的类只使用指向另一个的指针,所以你可以声明一个类:
class A;
class Bclass {
// ...
};
然后从那里开始。