Static constructor in c++ and fatal error LNK1120: 1 unresolved externals

[亡魂溺海] 提交于 2019-12-02 09:17:25

问题


To start with i should probably let you know that i am by no means a programmer, and i'm just doing this for a homework assignment, so if it's possible i will require a really detailed explanation :)

I currently have a Node class which i use to store the coordinates of points. Besides that, what i want to do with it is assign each different Node object an index number based on a counter. From what i gathered off the internet the way i do this is by using another class which initializes my counter in the constructor, and inside the Node class i just add it as a static parameter.

This is my code for the above:

class counter
{
  public:
    int nr;
    counter()
    {
        nr = 0;
    }
};

class Nod   
{  
  static counter ctr;

  public:   
     int index;      
     Punct pct;
     Nod(Punct &temp)
     {  
         pct = temp;    
         index = ctr.nr ++ ;
     }
     Nod() {}   
}; 

Now, that builds ok, but as soon as i try to declare a Nod object inside my main function i get the following error: fatal error LNK1120: 1 unresolved externals and i've got absolutely no clue as to why this is. I've previously gotten this error when trying to write my own destructor but i got around that by just deleting that bit of code.

Thank you, and sorry if i'm using the wrong terminology for some of the things i've referred to.


回答1:


A static data member is declared in a class, but it must be defined in exactly one translation unit (= .cpp file). Put the following into one .cpp file (preferably Nod.cpp):

counter Nod::ctr;

BTW, you could have just used an int instead of a custom class counter.




回答2:


I used to fall foul of that too. Then I read an article by Scott Meyers. He recommended a function static, rather than class static variable. This means you declare and define a variable all in one place. The following prints:

0 1 2 3 4 5 6 7 8 9

#include <iostream>

int next_index(void)
{
  static int index = 0;
  return index++;
}

int main(void)
{
  for (int i = 0; i < 10; ++i) {
    std::cout << next_index() << ' ';
  }
}

In your case, you'd put the following:

Nod(Punct &temp)
 {  
     pct = temp;    
     index = next_index();
 }



回答3:


The problem is that Nod::ctr is only declared but not defined.

class Nod
{
    // Declare the counter variable
    static counter ctr;

    ...
};

// Define the counter variable
counter Nod::ctr;

The definition should of course be in a source file, not a header file, or you will get multiple definition errors instead.




回答4:


From the description of your problem ,the solution is far simpler.

You have complicated a simple issue, just use

class Nod   
{  
static int ctr;

public: 
 int index;      
   Nod()
 {  
     ctr++;  //Increment the counter every time a object is created
 }
 Nod() {}   
}; 


//Allocate memory also

int Nod::ctr;

If there are more than 1 type of constructor, add the counter increment in every constructor.



来源:https://stackoverflow.com/questions/13706214/static-constructor-in-c-and-fatal-error-lnk1120-1-unresolved-externals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!