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

前端 未结 4 764
不知归路
不知归路 2021-01-23 23:36

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

4条回答
  •  孤街浪徒
    2021-01-23 23:43

    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 
    
    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();
     }
    

提交回复
热议问题