c++ - 如何在其他 `*.cpp` 文件中使用我的静态变量?
问题描述
我制作了文件,它们的名字是
main.cpp
able.hpp
able.cpp
ability.hpp
ability.cpp
在ability.hpp
文件中,有
static int num_abilities;
我不知道如何num_abilities
在able.cpp
. 我做了:
#include "ability.hpp"
另外,但我不能使用这个静态整数。我应该怎么办?
解决方案
static
global variables are file scope by default in C++.
Do:
- Change it to
extern
in the header file - Add a definition (without static) in a cpp file
You can then use your global variable across multiple files.
推荐阅读
- android - FireMonkey Android:引发异常类 EJNIException 并带有消息'java.lang.IndexOutOfBoundsException:setSpan(XX ... XX)超出长度 X
- c++ - 在 C++ 中,如何将字符串数组的元素分配给 char 数组?
- firebase - 从 Firestore 中检索图像
- swift - 每次触发时向 Swift 计时器传递不同的参数
- laravel - 如何通过 laravel 中的代码也将文件的扩展名发送到 gcloud 应用存储桶?
- python - 是否可以在 Plotly 中更改图例框的大小?
- listener - AWS CDK Python 将新的 ECS/Fargate 目标组添加到现有的应用程序负载均衡器 (ALB)
- firebase - 无法读取未定义的属性“部署”
- python - Python 和 Pandas:如何计算具有特定条件的列
- c# - 模型在 ASP.NET MVC 中回发时为空