简体   繁体   English

模板类的实例化是错误的

[英]The instantiation of the template class is wrong

The g++ compiler prompt the following statement is wrong: g ++编译器提示以下语句错误:

template<typename Type>
class SingleList{
public:
    SingleList()
        {
            head =  new SingleListNode<Type> () ;    //Error happens here!!!
        }

The error message is : 错误消息是:

 ./inc/single_list.h: In instantiation of ‘SingleListNode<int>’:
./inc/single_list.h|39 col 13| instantiated from ‘SingleList<Type>::SingleList() [with Type = int]’

The definition of head is the following, maybe problem is not related here. head的定义如下,也许问题与这里无关。

SingleListNode<Type> *head;

The instantiation of class SingleList in main function is : main函数中的SingleList类的实例为:

int main()
{
    SingleList<int> list;

I don't know where the syntax error happens, can anyone help me? 我不知道语法错误发生在哪里,有人可以帮我吗? Thanks!! 谢谢!!

======================================================================== ================================================== ======================

The following is the content of source file: 以下是源文件的内容:

    template<typename Type> class SingleList;

template<typename Type> class SingleListNode{
private:
    friend class SingleList<Type>;

    SingleListNode() : next(NULL){}

public:
    friend ostream& operator<< <Type>(ostream& os,SingleListNode<Type>& sln);             //Error here!!

private:
    SingleListNode *next;
};

template<typename Type> ostream& operator<<(ostream& os,SingleListNode<Type>& out){
    os<<out.data;
    return os;
}

template<typename Type> class SingleList{
public:
    SingleList()
        {
            head =  new SingleListNode<Type> () ;              //Error happens here.
        }
    ~SingleList(){
        delete head;                                       //Same error
    }

private:
    SingleListNode<Type> *head;
};

The error message prompted by g++ g ++提示的错误信息

|| g++ -g -I ./inc/ -c single_list_test.cpp  -o single_list_test.o
|| single_list.h: In instantiation of ‘SingleListNode<int>’:
single_list.h|25 col 13| instantiated from ‘SingleList<Type>::SingleList() [with Type = int]’
single_list_test.cpp|9 col 18| instantiated from here
single_list.h|10 col 18| error: template-id ‘operator<< <int>’ for ‘std::ostream& operator<<(std::ostream&, SingleListNode<int>&)’ does not match any template declaration
|| make: *** [single_list_test.o] Error 1

To my gcc , just modify the following statement 对于我的gcc,只需修改以下语句

friend ostream& operator<< <Type>(ostream& os,SingleListNode<Type>& sln)

to

 friend ostream& operator<< (ostream& os,SingleListNode<Type>& sln)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM