链接在代码块中不起作用!?(未定义的引用...
Linking doesn't work in codeblocks!? (undefined reference to ...)
我对代码块版本 10.05 有问题。我做了一个 c++ 项目,我写了一个这样的程序:
主.cpp
#include <iostream>
#include "vectorddd.hpp"
using namespace std;
int main()
{
vector3D<int> tesztinttomb;
tesztinttomb.saveout("igen.dat");
return 0;
}
头文件 (vectorddd.hpp):
#ifndef VECTORDDD_HPP_INCLUDED
#define VECTORDDD_HPP_INCLUDED
#include <iostream>
template <class T>
class vector3D {
T *x;
T *y;
T *z;
int meret;
public:
void saveout(char* FileName);
vector3D(int Meret=0) : x(new T[meret]), y(new T[Meret]), z(new T[Meret]), meret(Meret) {}
~vector3D() { delete [] x; delete [] y; delete [] z; }
};
#endif // VECTORDDD_HPP_INCLUDED
实现文件 (vectorDDD.cpp):
#include "vectorddd.hpp"
template <class T>
void vector3D<T>::saveout(char* FileName) {
int i=0;// I know this is stupid... but the emphasis is on the linking problem
}
它只是没有链接在一起。我知道我必须在属性>构建选项中检查.cpp文件链接和编译设置。而且我没有发现任何问题,只是写得总是一样:
In function `main':
undefined reference to `vector3D<int>::saveout(char*)'
||=== Build finished: 1 errors, 0 warnings ===|
如果我将.cpp文件实现放入我的 .hpp 文件中,它可以正常工作。但这不是代码块应该如何工作。
您的模板需要位于头文件中,请考虑一下,如果模板在 cpp 文件中,如何实例化模板?
你应该把这个:
template <class T>
void vector3D<T>::saveout(char* FileName) {
int i=0;// I know this is stupid... but the emphasis is on the linking problem
}
在您的标头 vectorddd.hpp 文件中
请参阅类似的 SO 帖子:C++在 .CPP 文件
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用