测试 LinkedLists 时 VS2010 编译错误
我正在对 LinkedList 进行一些测试,它有两个指针:一个指向列表中的下一项,另一个指向列表中的随机节点。
这是代码:
struct Node
{
Node* pNext; // a pointer to the next node in the list
Node* pReference; // a pointer to a random node within the list
int number; // an integer value
};
/**
* This version works for small/medium lists, using recursion.
*/
Node* duplicateList(Node* n)
{
if (n == NULL) return NULL;
return new Node()
{
number = n->number,
pNext = duplicateList(n->pNext),
pReference = duplicateList(n->pReference)
};
}
我收到以下错误(VS2010):
d:\dornad\my Documents\visual studio 2010\projects\test\test.cpp(21): 错误 C2143: 语法错误:缺少 ' ;'在“{”之前
1>d:\dornad\my Documents\Visual Studio 2010\projects\test\test.cpp(22): 错误 C2065: 'number' : 未声明的标识符
<代码>1>d:\dornad\my Documents\Visual Studio 2010\projects\test\test.cpp(23): 错误 C2065: 'pNext': 未声明的标识符
1>d:\dornad\我的文档\ Visual Studio 2010 \ Projects \ test \ test.cpp(24):错误C2065:'pReference':未声明的标识符
1> d:\dornad \我的文档\ Visual Studio 2010 \项目\ test\test.cpp(25): 错误 C2143: 语法错误: 缺少 ';'在“}”之前
谢谢。
I'm doing some tests to a LinkedList, that has two pointers: one point to the next item in the list and the other point to a random node within the list.
Here is the code:
struct Node
{
Node* pNext; // a pointer to the next node in the list
Node* pReference; // a pointer to a random node within the list
int number; // an integer value
};
/**
* This version works for small/medium lists, using recursion.
*/
Node* duplicateList(Node* n)
{
if (n == NULL) return NULL;
return new Node()
{
number = n->number,
pNext = duplicateList(n->pNext),
pReference = duplicateList(n->pReference)
};
}
I'm getting the following errors (VS2010):
d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(21): error C2143: syntax error : missing ';' before '{'
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(22): error C2065: 'number' : undeclared identifier
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(23): error C2065: 'pNext' : undeclared identifier
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(24): error C2065: 'pReference' : undeclared identifier
1>d:\dornad\my documents\visual studio 2010\projects\test\test.cpp(25): error C2143: syntax error : missing ';' before '}'
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该位不是有效的 C++:
将其更改为:
This bit is not valid C++:
Change it to this:
向 Node 添加一个构造函数:
然后
Add a constructor to Node:
then