创建类链接错误 [英] creating classes link error
问题描述
我使用的是Dev-C ++ 5.2.0.1
I'm using Dev-C++ 5.2.0.1
我举了一个例子,如何把一个类放在另一个文件从网站,但它导致错误。
I took an example of how to put a class in another file from a website but it resulted in an error.
在文件class.h中有:
In the file class.h I have:
class MyClass
{
public:
void foo();
int bar;
};
在文件class.cpp中有:
In the file class.cpp I have:
#include "class.h"
void MyClass::foo()
{
cout<< "test";
}
在文件main.cpp中有:
In the file main.cpp I have:
#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()'
collect2: ld退回1退出状态
Here is the error I get: [Linker error] C:\Users\Matthew\AppData\Local\Temp\cccWe7ee.o:main.cpp:(.text+0x16): undefined reference to `MyClass::foo()' collect2: ld returned 1 exit status
有什么我做错了吗?
推荐答案
foo()只有定义。如果你想使用没有实现的函数,Linker会给你这个错误未定义的引用。
foo() only have definition. If you want to use a function that doesn't have a implement, Linker will give you this error "undefined reference".
这篇关于创建类链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!