我遇到了codeblocks ver 10.05的问题。我做了一个c ++项目,我写了一个这样的程序:
的main.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文件中,它可以正常工作。但这不是代码块应该如何工作的。
答案 0 :(得分:2)
您的模板需要在头文件中,考虑一下,如果模板位于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帖子:Storing C++ template function definitions in a .CPP file