【发布时间】:2013-09-19 16:49:25
【问题描述】:
using namespace std;
#include <vector>
#include <string>
template <class T>
struct ValNode {
string id;
T value;
};
class ValTable {
public:
ValTable();
template <class T>
void add(string,T);
const bool find(string);
void remove(string);
private:
template<class T>
std::vector<ValNode<T>*> vals;
};
编译器错误:error: data member 'vals' cannot be a member template
我确实尝试在结构中使用 T* 值,但没有成功。 我还没有使用代码中的任何功能。只是试图将它编译成 *.o 文件(也带有 .cpp 文件)。
【问题讨论】:
-
你不能那样做。
ValTable必须是模板,否则您必须添加另一个包含向量的模板类。 -
您希望编译器如何确定
T应该在vals的声明中是什么?制作整个班级模板。请记住,模板类通常不能在 .cpp 文件中定义,它们必须在标题中内联定义。