错误:转换无效

时间:2013-11-16 00:56:59

标签: c++ compiler-errors linked-list

所以我有一个节点类:

template <typename Type>
class NodeType
{
    public:
    Type m_data;
    NodeType<Type> *mp_next;
    // note data goes uninitialized for default constructor
    // concept being Type's constructor would auto-init it for us
    NodeType() { mp_next = NULL; }
    NodeType(Type data) {m_data = data; mp_next = NULL;}
};

我正在尝试创建一个像这样的新节点:

NodeType<int> n1 = new NodeType<int>(5);

编译器告诉我:

SLTester.cpp:73:40: error: invalid conversion from ‘NodeType<int>*’ to ‘int’ [-fpermissive]
SingList.h:29:2: error:   initializing argument 1 of ‘NodeType<Type>::NodeType(Type) [with Type = int]’ [-fpermissive]

任何人都可以帮我弄清楚为什么会发生这种情况和/或我实际应该做些什么?

1 个答案:

答案 0 :(得分:3)

通过定义NodeType<int> n1n1不是指针类型,

更新

NodeType<int> n1 = new NodeType<int>(5);

为:

NodeType<int> n1{5};

NodeType<int> n1(5);