首页 > 解决方案 > 如何在其他 `*.cpp` 文件中使用我的静态变量?

问题描述

我制作了文件,它们的名字是

ability.hpp文件中,有

static int num_abilities;

我不知道如何num_abilitiesable.cpp. 我做了:

#include "ability.hpp"

另外,但我不能使用这个静态整数。我应该怎么办?

标签: c++

解决方案


static global variables are file scope by default in C++.

Do:

  1. Change it to extern in the header file
  2. Add a definition (without static) in a cpp file

You can then use your global variable across multiple files.


推荐阅读