Link error when declaring public static variables

2019-02-09 15:02发布

问题:

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.

回答1:

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.



回答2:

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.