Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link error when declaring public static variables in C++

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.

like image 618
Zhertal Avatar asked Dec 12 '11 19:12

Zhertal


People also ask

Can static variables be public?

Just like an instance variables can be private or public, static variables can also be private or public.

How do you declare static variables?

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.

What happens when a variable is declared static?

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.

What is declaring static?

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.


2 Answers

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.

like image 39
Jonathan Leffler Avatar answered Sep 19 '22 14:09

Jonathan Leffler


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.

like image 64
Armen Tsirunyan Avatar answered Sep 20 '22 14:09

Armen Tsirunyan