试图用C++构造简单的测试类
Trying to construct simple test class in C++
我很难理解我遇到的一些错误。我用一个驱动程序构造了一个简单的Test类。有人能指出我犯的错误吗?
在这里,我试图创建一个Test对象,并将数字变量设置为1,然后打印数字变量。
驱动程序:
#include "test.h"
#include <iostream>
using namespace std;
int main() {
Test *myTest = new Test(1);
cout << myTest->getNumber();
return 0;
}
test.h
#ifndef __TEST_H__
#define __TEST_H__
class Test
{
private:
int number;
public:
Test();
Test(int theNumber);
int getNumber();
};
#endif
test.cpp
#include "test.h"
Test() {
}
Test(int aNumber) {
number = aNumber;
}
int getNumber() {
return number;
}
我得到的错误是
> Undefined symbols for architecture x86_64: "Test::getNumber()",
> referenced from:
> _main in cc8cXu6w.o "Test::Test(int)", referenced from:
> _main in cc8cXu6w.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
感谢
在类外定义类成员时,应该使用类作用域。
Test::Test(){
}
Test::Test(int aNumber){
//...
}
int Test::getNumber(){
//...
}
另外,别忘了编译并链接test.cpp。只编译main.cpp(或调用任何驱动程序源文件)也可能导致这样的链接错误。
如果使用GCC,请使用以下命令进行构建:
g++ -o test main.cpp test.cpp
相关文章:
- 处理简单 cpp 类构造函数中的错误
- 使用gtest时,在头文件中而不是.cpp文件中具有可测试类的利弊
- googlemock-如何模拟被测试类所拥有的对象
- 如何使用回调使工作变得简单按钮类?
- 返回值的简单模板类成员函数专用化
- 单元测试类无法在我要测试的同一解决方案中的另一个项目中找到类
- 创建一个简单的类及其变量和函数不断返回" segmentation fault (core dumped)"、C++
- 简单派生类上的Linux GCC链接错误
- CMake 在尝试编译简单测试程序时失败
- gmock EXPECT_CALL 不会在测试类中调用该方法
- 简单矩阵类,错误:调用私有构造函数
- 使用专业化来测试类中值的存在
- 在C 中,是否有一种(新的)方法来测试类是否在模板中具有虚拟破坏者而无需实例化实例
- 简单包装类与智能指针
- 具有重载<<运算符的简单模板类失败,"Invalid use of template-id"
- Boost单元测试夹具继承测试类,是否可以访问受保护的方法
- 声明一个简单模板类的variadic模板类的朋友
- 简单的类构造函数初始化错误
- C 未解决的外部词,具有简单的类定义
- 试图用C++构造简单的测试类