[英]Why I am getting “Fatal error LNK1120: 1 unresolved externals”
我正在使用Node
类来创建linked list
的node
。 它实现简单,不完整。 我first
使用指针作为静态我认为这将是更好的选择,因为我将使用它一次。 那就是我要存储第一个Node
地址。 但是当我尝试编译时,我得到了以下错误。
1> main.obj:错误LNK2001:未解析的外部符号“public:static class Node * Node :: first”(?first @ Node @@ 2PAV1 @ A)1> c:\\ users \\ labeeb \\ documents \\ visual studio 2010 \\ Projects \\ linked list1 \\ Debug \\ linked list1.exe:致命错误LNK1120:1个未解析的外部因素==========构建: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;
}
将此行从main
内部移动到文件范围,并稍微更改它:
Node* Node::first = NULL;
如上所述,您将声明一个名为first
的局部变量,类型为“指向Node
类成员的指针,类型为Node
”。 此局部变量与Node::first
不同且不相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.