在C ++中声明公共静态变量时出现链接错误 [英] Link error when declaring public static variables in C++
问题描述
我有一个带有可变配置参数的类.我想将其包含在其他类中:JugadorHumano
,JugadorIA
,Main
,PartidaClasica
,PartidaMision
.
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.
推荐答案
您应该在cpp文件中写入定义,否则,一旦将头文件包含到多个C ++文件(转换单元)中,您将获得重新定义错误. #pragma一次只能在一个翻译单元内运行.因此,您需要一个具有以下内容的Configuration.cpp文件
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.
这篇关于在C ++中声明公共静态变量时出现链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!