我正在尝试制作一个战锤 40K 军队计数器程序,这样我就不必继续使用 excel。我的问题是我的程序出错,因为我有两个不同的对象和它们的头文件具有相同的变量名。
当我试图运行我的程序时,g++ 提示该变量已经在我的第一个对象中声明。
我要怎么做才能避免这种情况发生?
这是 SKulltaker.cpp 程序的代码片段
#include "SkullTaker.h"
#include <string>
int pointCost = 140;
int minSize = 1;
int maxSize = 1;
std::vector<std::string> rules;
这是 SkullTaker.h
class SkullTaker {
public:
SkullTaker();
SkullTaker(const SkullTaker& orig);
int getPointCost();
int getMinSize();
int getMaxSize();
std::vector<std::string> getRules();
std::string toString();
virtual ~SkullTaker();
};
我的其他类是相同的,但 KuGath 中的 .cpp 和 .h 文件的名称。
g++ 提示 PointCost 有多个定义。
谢谢,
多哈
最佳答案
使用“static”关键字表明您的全局变量的范围应仅限于它的翻译单元。
static int pointCost = 140;
关于c++ - 如何在 C++ 中的两个不同对象中使用相同的变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11996276/