<分区>
我在 hpp 文件中定义了一个类,我正试图在另一个 header 中使用它,因此我对它做了一个前向声明(我只想通过引用将其用作函数参数)。出于某种原因,我不断收到编译错误。我不知道为什么它不起作用。这是我的代码:
//something.hpp:
class MyClass;
void someFunction (MyClass& mc);
...
//something.cpp:
#include "MyClass.hpp"
void someFunction (MyClass& mc) {...}
...
//MyClass.hpp:
class MyClass {
const char* myText;
public:
MyClass (const char* text) : myText(text) {}
};
//main.cpp:
int main () {
...
someFunction (MyClass ("some text here"));
...
}
我从 main() 得到一个错误,它说:
'<function-style-cast>' : cannot convert from 'const char [15]' to 'MyClass'
Source or target has incomplete type
如果我理解正确,这意味着编译器没有找到 MyClass 的定义,只有它的预声明(即使我在 something.cpp 中包含了 MyClass.hpp),这就是为什么它说它不完整。我在这里错过了什么?