我正试图为我的班级使用模板。当我按下“运行”按钮时,会得到以下错误:
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++模板时遇到了问题。我是新来的,我不知道该怎么办。
发布于 2014-12-08 22:33:53
您的代码无效。内联定义成员函数时,不重复类名。那就是,你有
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,而不是尝试创建自己的。
https://stackoverflow.com/questions/27368196
复制相似问题