【发布时间】:2011-04-06 05:12:43
【问题描述】:
我刚开始用 C++ 编程,我尝试创建 2 个类,其中一个将包含另一个。
文件A.h:
#ifndef _A_h
#define _A_h
class A{
public:
A(int id);
private:
int _id;
B _b; // HERE I GET A COMPILATION ERROR: B does not name a type
};
#endif
文件A.cpp:
#include "A.h"
#include "B.h"
#include <cstdio>
A::A(int id): _id(id), _b(){
printf("hello\n the id is: %d\n", _id);
}
文件B.h:
#ifndef _B_h
#define _B_h
class B{
public:
B();
};
#endif
文件B.cpp:
#include "B.h"
#include <cstdio>
B::B(){
printf("this is hello from B\n");
}
我先编译了 B 类,然后编译了 A 类,然后我得到了错误信息:
A.h:9:错误:“B”没有命名类型
我该如何解决这个问题?
【问题讨论】:
-
@Georg 为什么你把所有东西都放在一个代码段中?它们是不同的文件。
-
@Amir: 在我点击 edit 之前它看起来已经坏了,我心不在焉:)
-
您可以通过单击答案旁边的勾号来接受您认为最有用的答案之一。这将有助于其他遇到类似问题的人。
-
@Naveen 有一个最短时间,所以还没有:P
-
我把所有东西都放在一个代码段中,因为一开始根本没有任何代码段。
标签: c++