【发布时间】:2014-03-26 13:19:17
【问题描述】:
我正在使用Node 类创建linked list 的node。它实现简单且不完整。我将指针first 用作静态我认为它会是更好的选择,因为我将使用它一次。那就是我要存储第一个Node的地址的时候。但是当我尝试编译时出现以下错误。
1>main.obj:错误 LNK2001:无法解析的外部符号“public:static 类节点 * 节点::first" (?first@Node@@2PAV1@A) 1>c:\users\labeeb\documents\visual studio 2010\Projects\linked list1\Debug\linked list1.exe : 致命错误 LNK1120: 1 unresolved 外在 ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========
注意:我使用的是 Visual C++ 2010。
代码:
#include<iostream>
using namespace std;
class Node
{
public:
static Node *first;
Node *next;
int data;
Node()
{
Node *tmp = new Node; // tmp is address of newly created node.
tmp->next = NULL;
tmp->data = 0; //intialize with 0
Node::first = tmp;
}
void insert(int i)
{
Node *prev=NULL , *tmp=NULL;
tmp = Node::first;
while( tmp->next != NULL) // gets address of Node with link = NULL
{
//prev = tmp;
tmp = tmp->next;
}
// now tmp has address of last node.
Node *newNode = new Node; // creates new node.
newNode->next = NULL; // set link of new to NULL
tmp->next = newNode; // links last node to newly created node.
newNode->data = i; // stores data in newNode
}
};
int main()
{
Node Node::*first = NULL;
Node n;
system("pause");
return 0;
}
【问题讨论】:
标签: c++ debugging visual-c++ linked-list