我正在使用Node
類創建node
的linked list
。這是簡單的實施和不完整的。我使用指針first
作爲靜態我認爲這將是更好的選擇,因爲我打算使用它一次。那就是當我要存儲第一個Node
的地址時。但是當我嘗試編譯時,我收到以下錯誤。爲什麼我得到「致命錯誤LNK1120:1個未解決的外部」
1> main.obj:錯誤LNK2001:解析外部符號 「公共:靜態 類節點*節點::第一」 1> C(第一@ @@節點@ 2PAV1 A 1):\用戶\ labeeb \ documents \ visual studio 2010 \ Projects \ linked list1 \ Debug \ linked list1.exe:致命錯誤LNK1120:1無法解析 外部 ==========構建:0成功,1失敗,0失敗-to-日期,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;
}