首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >致命错误C1001:试图使用模板的内部错误

致命错误C1001:试图使用模板的内部错误
EN

Stack Overflow用户
提问于 2014-12-08 22:30:15
回答 1查看 914关注 0票数 0

我正试图为我的班级使用模板。当我按下“运行”按钮时,会得到以下错误:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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++模板时遇到了问题。我是新来的,我不知道该怎么办。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-08 22:33:53

您的代码无效。内联定义成员函数时,不重复类名。那就是,你有

代码语言:javascript
复制
void List<T>::add(T * a){
    last->next = a;
    last = a;
}

你想要的

代码语言:javascript
复制
void add(T * a){
    last->next = a;
    last = a;
}

或者,可以将成员函数定义从类定义中移出:

代码语言:javascript
复制
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::vectorstd::list,而不是尝试创建自己的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27368196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档