对类方法的未定义引用
Undefined reference to class methods
我有三个文件method.h、method.cpp、main.cpp
method.h
#ifndef METHOD_H
#define METHOD_H
class method {
public:
void printThisMethod();
private:
};
#endif
方法.cpp
#include "method.h"
inline void method::printThisMethod() {
//some methods done here
}
main.cpp
#include <iostream>
#include <string>
#include "method.h"
int main() {
method outputMethod;
outputMethod.printThisMethod;
}
我收到错误,
undefined reference to method::printThisMethod.
请帮忙,谢谢
删除inline
关键字,或将定义移动到标头中(保留inline
)。
inline
用于放宽"一个定义规则"以允许在标头中进行定义。然而,也要求在每个使用它的翻译单元中都有一个定义,这通常要求定义在页眉中。
如果没有inline
,则应用正常的链接规则,并且在一个翻译单元中必须有一个单独的定义。如果您从现有代码中删除inline
,那么这就是您所拥有的。
(您还需要在函数调用outputMethod.printThisMethod()
中添加括号,但据推测,您的真实代码中有括号,否则就不会出现链接错误。)
您需要更改
outputMethod.printThisMethod;
至
outputMethod.printThisMethod();
相关文章:
- 编译C++时未定义的引用
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 未定义的引用在哪里
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 对 Scalar ::Scalar() 的未定义引用
- 已定义函数时出现 G++ "未定义的引用"错误
- 链接 cmake 时出现未定义的引用错误
- 对复制 CTOR 和 CTOR 的未定义引用
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 右值引用未定义的行为
- 对"vtable for DigitalClock"的未定义引用 - 未定义对"DigitalClock::staticMetaObject"的引用 - Qt
- 函数类型到函数类型的引用:未定义的引用
- avcodec_alloc_context引用未定义,但ffmpeg链接顺序正确
- 从c++调用Fortran子程序,链接时引用未定义
- 枚举引用未定义,但位于包含的头文件中