I have this class with variable configuration parameters. 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;
What I want is to be able to modify or read the values from the other classes. I can't declare a static variable and define it in the declaration. I can't let that variables without 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 redefinition I get? I can't figure it out and I can't find a similar problem.
Just like an instance variables can be private or public, static variables can also be private or public.
C++ Programming Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. There would only be one copy of each class variable per class, regardless of how many objects are created from it.
When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable.
Similarly, a static method – a method declared as static inside a class definition – is meant to be relevant to all instances of a class rather than any specific instance. A method declared as static can be called without instantiating the class.
Place the variable definitions in a source file and compile and link that separately.
The header should only contain declarations and inline functions.
The #pragma once
protects a single TU (translation unit); it provides no protection against multiple independent TUs including (and therefore defining) the same variable.
You should write the definitions in the cpp file, otherwise, once you include your header file into more than one C++ file(translation unit), you'll get redefinition errors. And #pragma once operates only within one translation unit. So you need a Configuration.cpp file with the following contents
#include "Configuracion.h"
int Configuracion::MAX_ATAQUES = 5;
int Configuracion::DIV_TERRITORIOS = 3;
Also, if your class contains only static member, you have the option of considering having a namespace instead of a class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With