【发布时间】:2013-10-24 01:07:20
【问题描述】:
我很困惑……
这是 *.h 文件:
#include <string>
#include <vector>
template<typename T>
class pvector
{
private:
std::string filename;
std::vector<T> v;
void readvector();
public:
pvector(std::string fname) : filename(fname) { readvector(); }
void push_back(const T &el) { v.push_back(el); }
void pop_back() { v.pop_back(); }
};
这是 *.cpp:
#include <fstream>
#include <string>
#include "pvector.h"
using namespace std;
template<typename T>
void pvector<T>::readvector()
{
ifstream ifs(filename);
for(;;)
{
T x; ifs >> x; if(!ifs.good()) break;
v.push_back(x);
}
}
如果我想生成以下对象:
pvector<string> myVec("testfile.txt");
...我明白了:
pvector.h:24: 对 `pvector::readvector()' 的未定义引用
为什么???
【问题讨论】:
-
stackoverflow.com/questions/18543980/… 这个答案正是你想要的(我也为此感到自豪),它既解释了错误,也有解决方法。
-
非常感谢您的提示...我目前正在研究模板,并没有意识到这一点。
标签: c++ eclipse templates undefined-reference