我试图为我的班级使用模板。当我点击运行按钮时,出现以下错误:
1>path\to\the\project\list.cpp(21): fatal error C1001: An internal error has occurred in the compiler.
1> (compiler file 'msc1.cpp', line 1443)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1> path\to\the\project\list.cpp(48) : see reference to class template instantiation 'List<T>' being compiled
这是我的list.cpp文件:
template<class T>
class List{
public :
T * first;
T * last;
List<T>::List(){
first = new T();
first->makeRef();
last = first;
}
void List<T>::add(T * a){
last->next = a;
last = a;
}
void List<T>::recPrint(){
recPrint(1);
}
void List<T>::recPrint(int id){
T * p = first;
int i=id;
while(!p){
p->recPrint(i++);
p = p->next;
}
}
};
好像我在使用c plus plus模板时遇到了问题。我是新手,我不知道该怎么做。
答案 0 :(得分:1)
您的代码无效。在内联定义成员函数时,不会重复类名。也就是说,你在哪里
void List<T>::add(T * a){
last->next = a;
last = a;
}
你想要
void add(T * a){
last->next = a;
last = a;
}
或者,您可以将成员函数定义移出类定义:
template<class T>
class List{
public :
/* ... */
void add(T * a);
/* ... */
};
template <typename T>
void List<T>::add(T * a){
last->next = a;
last = a;
}
那就是说,你真的想使用std::vector
或std::list
而不是试图制作自己的。{/ p>