我是 C++ 的新手,但我确实有一些 Java 经验。 在编码时,我偶然发现了一个让我困惑的错误。 这是我的代码(经过简化,但错误相同):
A.h:
#pragma once
#include "B.h"
class A
{
public:
A();
void foo();
void sayHello();
B b;
};
A.cpp:
#include "A.h"
#include <iostream>
A::A() {}
void A::foo() {
b.bar(this);
}
void A::sayHello() {
std::cout << "Hello" << std::endl;
}
B.h:
#pragma once
#include "A.h"
class B
{
public:
B();
void bar(A *a);
};
B.cpp:
#include "B.h"
B::B(){}
void B::bar(A *a) {
a->sayHello();
}
我想将 a 对象的指针传递给 B 中的 bar 函数,这样我就可以修改并在 bar 中访问 a 的字段。奇怪的是,当我通过另一个类的 A 实例调用 foo 时,我得到了这些错误:
1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1> main.cpp
1>d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A'
1> B.cpp
1>d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C3646: 'b': unknown override specifier
1>d:\stuff\visual studio 2015\projects\test\test\a.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> A.cpp
1>d:\stuff\visual studio 2015\projects\test\test\b.h(7): error C2061: syntax error: identifier 'A'
1>d:\stuff\visual studio 2015\projects\test\test\a.cpp(5): error C2660: 'B::bar': function does not take 1 arguments
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
如果我不在 B.h 中包含 A.h 并且我不向 bar 函数传递任何内容,则代码可以正常工作。
我尝试用谷歌搜索可能导致这些错误的原因,但我无法自行解决问题,因为我不明白是什么导致了这些错误。我做错了什么?
最佳答案
在A.h的头文件中,你有:
#include "B.h"
在B.h的头文件中,你有:
#include "A.h"
您有一个循环包含,其中 A
和 B
的定义相互依赖。
一个简单的解决方案是使用 forward declaration在 B 类
的定义中:
- 将文件 B.h 中的
#include "A.h"
行替换为class B;
- 在B.cpp 开头添加
#include "A.h"
但是请注意,您不能对 class A
使用前向声明,原因是您有 class B
的值 b
作为 a A类
的成员变量:
#pragma once
#include "B.h"
class A
{
public:
A();
void foo();
void sayHello();
B b; /// I mean this line here more specifically
};
编译器需要知道 class B
的定义才能确定 class A 的正确大小。这就是为什么你必须将 #include "B.h"
在 A.h 的开头。
关于C++:将 this 指针传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36345473/