创建类链接错误
creating classes link error
我正在使用 Dev-C++ 5.2.0.1
我举了一个如何将类放在网站的另一个文件中的示例,但它导致了错误。
在文件类.h中,我有:
class MyClass
{
public:
void foo();
int bar;
};
在文件类中.cpp我有:
#include "class.h"
void MyClass::foo()
{
cout<< "test";
}
在文件主中.cpp我有:
#include "class.h"
using namespace std;
int main()
{
MyClass a;
a.foo();
return 0;
}
这是我得到的错误: [链接器错误]C:\Users\Matthew\AppData\Local\Temp\cccWe7ee.o:main.cpp:(.text+0x16): 对'MyClass::foo()' 的未定义引用 收集2:LD 返回 1 个退出状态
我做错了什么吗?
新答案。
您是否正在编译所有文件并将其链接在一起?在 gcc 中,您可以执行以下操作:
gcc -o myExe class.cpp main.cpp
我对 dev-c++ 不太确定,但我想它没有太大区别。
foo() 只有定义。如果要使用没有实现的函数,Linker 会给你此错误"未定义的引用"。
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 使用cmake错误链接boost日志
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- 静态库中的 g++ 错误链接函数
- 未定义的参考错误链接天然函数Android
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- 聚输出错误 - 链接列表
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 尝试构建C DLIB示例无法与数百个未定义的参考错误链接