C ++ Duplicate Symbol error when defining a static class variable in Xcode

I have a static class member magnified in the constructor. According to the rules, it is declared in the class and defined outside. This must be completely legal. Any ideas why I get duplicate character error?

class Player
{
   private:
      static int numPlayers;
   public:
      Player() { numPlayers++; }
};

int Player::numPlayers = 0;
+3
source share
2 answers

The problem is that you are not separating your DECLARATION from your DEFINITION. Consider:

class player
{
   private:
      static int numPlayers;
   public:
      Player () {numPlayers ++; }
};

"numPlayers" "Player". - "Player:: numPlayers". :

int Player::numPlayers = 0;

- - Player:: numPlayers . , . , ...

".c", ".cpp", ".m", ".mm" "" (.. , DEFINITIONS). "" (.. , ). , Xcode, . , - , , , .

Xcode... , ; , . ".mm" , ".h", , ".h" , .

+9

? .. cpp.

+1

Source: https://habr.com/ru/post/1724918/


All Articles