C++ 对“test::setTest(int)和#x27”的未定义引用;
当我编译这段代码时,有两个错误“未定义对test::setTest(int)的引用”和“未定义对test::getTest()的引用”。我不知道出了什么问题,我正在arch linux中使用g++编译器: 测试hC++ 对“test::setTest(int)和#x27”的未定义引用;,c++,C++,当我编译这段代码时,有两个错误“未定义对test::setTest(int)的引用”和“未定义对test::getTest()的引用”。我不知道出了什么问题,我正在arch linux中使用g++编译器: 测试h class test{ int i; public : void setTest(int); int getTest(); }; test.cpp #include<iostream> #include"test.h" vo
class test{
int i;
public :
void setTest(int);
int getTest();
};
test.cpp
#include<iostream>
#include"test.h"
void test :: setTest(int x){
i = x;
}
int test :: getTest(){
return i;
}
#包括
#包括“test.h”
无效测试::setTest(int x){
i=x;
}
int测试::getTest(){
返回i;
}
mainTest.cpp
#include<iostream>
#include"test.h"
using namespace std;
int main(){
test t;
t.setTest(5);
cout<< "the value of i is : "<<t.getTest();
}
#包括
#包括“test.h”
使用名称空间std;
int main(){
试验t;
t、 setTest(5);
cout您没有同时编译这两个文件,因此输出可执行文件没有对类的方法的引用,只在您的终端上执行以下操作:
g++ mainTest.cpp test.cpp -o mainTest
然后按以下方式运行:
./mainTest
如何生成?您使用两个源文件(或对象文件)进行生成?首先,我生成mainTest.cpp并获得2个错误,然后生成test.cpp,但再次出错您在生成时是否尝试将两个源文件作为参数传递?Internet上可能有成千上万个示例(包括此处的许多示例)这告诉您如何使用多个源文件构建程序,只需使用普通的GCC。尝试同时编译两个文件:g++mainTest.cpp test.cpp-o mainTest
,然后运行/mainTest
是的,我同时编译了这两个文件,结果正常,tnx