【问题标题】:Why I am getting "Fatal error LNK1120: 1 unresolved externals"为什么我收到“致命错误 LNK1120:1 个未解决的外部问题”
【发布时间】:2014-03-26 13:19:17
【问题描述】:

我正在使用Node 类创建linked listnode。它实现简单且不完整。我将指针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


    【解决方案1】:

    将此行从main 内部移动到文件范围,并稍作更改:

    Node* Node::first = NULL;
    

    正如所写,您正在声明一个名为first 的局部变量,其类型为“指向Node 类成员的指针,类型为Node”。此局部变量与Node::first 不同且无关。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 2013-07-10
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      相关资源
      最近更新 更多