【发布时间】:2015-04-18 20:32:19
【问题描述】:
错误
t.cpp: 在构造函数 'SinglyLinkedList::SinglyLinkedList(T*, size_t) [with T = int]': t.cpp:29: 从这里实例化 第 51 行: 错误:无法将 'SinglyLinkedList::node*' 转换为 'int*' in 赋值编译由于 -Wfatal-errors 而终止。
在下面显示的行中
node * lastNode = new node;
lastNode->val = *arr;
lastNode->next = NULL;
for (T * pa(arr+1), * pb(arr+n); pa != pb; ++pa)
{
node * thisNode = new node;
thisNode->val = *pa;
thisNode->next = NULL;
lastNode->next = thisNode; // error
lastNode = thisNode;
delete thisNode;
}
完整代码在这里:http://codepad.org/gZ2KnrUM
无法弄清楚该行在语法上的错误之处。
额外问题:有没有办法用new 对struct 进行简写初始化?我希望能够做出类似的线条
node * lastNode = new node;
lastNode->val = *arr;
lastNode->next = NULL;
如果可能的话,放在一行中。我知道如果我在堆栈上创建它,那么我可以这样做
node lastNode = { *arr, NULL m};
但是是否有一个等效的大括号初始化用于使用new 创建?
【问题讨论】:
-
创建节点的快捷方式是使用与以前相同的语法,但将
new node放在前面