Link error when declaring public static variables in C++

北战南征 提交于 2019-11-30 13:07:59

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!