所以我有一个节点类:
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]
任何人都可以帮我弄清楚为什么会发生这种情况和/或我实际应该做些什么?
答案 0 :(得分:3)
通过定义NodeType<int> n1
,n1
不是指针类型,
更新
NodeType<int> n1 = new NodeType<int>(5);
为:
NodeType<int> n1{5};
或
NodeType<int> n1(5);