user3461957
user3461957

Reputation: 163

Why I am getting "Fatal error LNK1120: 1 unresolved externals"

I am using Node class to create a node of linked list. It is simple implementation and incomplete. I used pointer first as static I thought it would be better choice as I am going to use it once. And that is when I am going to store address of first Node. but when I try to compile I am getting following error.

1>main.obj : error LNK2001: unresolved external symbol "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 : fatal error LNK1120: 1 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Note: I am using Visual C++ 2010.

Code:

#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;
}

Upvotes: 0

Views: 1843

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52621

Move this line from inside main to file scope, and change it slightly:

Node* Node::first = NULL;

As written, you are declaring a local variable named first, of type "pointer to member of class Node, with type Node". This local variable is distinct from and unrelated to Node::first.

Upvotes: 2

Related Questions