Communication error when declaring public static variables in C ++

I have this class with variable configuration options. I want to include it in other classes: JugadorHumano , JugadorIA , Main , PartidaClasica , PartidaMision .

 #pragma once class Configuracion { public: static int MAX_ATAQUES; static int DIV_TERRITORIOS; }; int Configuracion::MAX_ATAQUES = 5; int Configuracion::DIV_TERRITORIOS = 3; 

I want to be able to modify or read values ​​from other classes. I cannot declare a static variable and define it in the declaration. I cannot allow these variables without a definition either because I get "Unresolved External" errors.

 1>JugadorIA.obj : error LNK2005: "public: static int Configuracion::MAX_ATAQUES" \ ( ?MAX_ATAQUES@Configuracion @@2HA) already defined in JugadorHumano.obj 1>JugadorIA.obj : error LNK2005: "public: static int Configuracion::DIV_TERRITORIOS" \ ( ?DIV_TERRITORIOS@Configuracion @@2HA) already defined in JugadorHumano.obj 1>Main.obj : error LNK2005: "public: static int Configuracion::MAX_ATAQUES" \ ( ?MAX_ATAQUES@Configuracion @@2HA) already defined in JugadorHumano.obj 1>Main.obj : error LNK2005: "public: static int Configuracion::DIV_TERRITORIOS" \ ( ?DIV_TERRITORIOS@Configuracion @@2HA) already defined in JugadorHumano.obj 1>PartidaClasica.obj : error LNK2005: "public: static int Configuracion::MAX_ATAQUES" \ ( ?MAX_ATAQUES@Configuracion @@2HA) already defined in JugadorHumano.obj 1>PartidaClasica.obj : error LNK2005: "public: static int Configuracion::DIV_TERRITORIOS" \ ( ?DIV_TERRITORIOS@Configuracion @@2HA) already defined in JugadorHumano.obj 1>PartidaMision.obj : error LNK2005: "public: static int Configuracion::MAX_ATAQUES" \ ( ?MAX_ATAQUES@Configuracion @@2HA) already defined in JugadorHumano.obj 1>PartidaMision.obj : error LNK2005: "public: static int Configuracion::DIV_TERRITORIOS" \ ( ?DIV_TERRITORIOS@Configuracion @@2HA) already defined in JugadorHumano.obj 1>D:\Leire\My Dropbox\Carpetas compartidas\Compartidos Victor\Practicas POO II\P3\P3M10\Debug\P3M10.exe : fatal error LNK1169: one or more multiply defined symbols found 

What should I do to avoid this override that I get? I cannot understand this, and I cannot find a similar problem.

+7
source share
2 answers

You must write the definitions in the cpp file, otherwise, as soon as you include your header file in several C ++ files (translation unit), you will get override errors. And #pragma only works within one translation unit. Therefore, you need a Configuration.cpp file with the following contents

 #include "Configuracion.h" int Configuracion::MAX_ATAQUES = 5; int Configuracion::DIV_TERRITORIOS = 3; 

In addition, if your class contains only a static member, you have the opportunity to consider using a namespace instead of a class.

+12
source

Place the variable definitions in the source file and compile and link them.

The title should only contain declarations and built-in functions.

#pragma once protects one translation unit (TU); it does not provide protection against multiple independent TUs, including (and therefore defining) the same variable.

+3
source

All Articles