解决全球
我试图让对象引用一些全局变量(在你开始攻击我使用全局变量之前,将它们放入一个对象中要么会使其无法被其他对象访问,要么需要在方法调用中添加多余的参数)例如
main.cpp
bool firstLoop;
const int dt = 10;
的事情.cpp
void thing::Update(Object * thingToUpdate){
if( firstLoop){
...
} else{
// working with dt
}
}
但每次我尝试访问变量时都会收到“未声明的标识符”错误 我需要将它们标记为外部或其他什么吗?
I am trying to get objects to reference a few globalized variables (before you start hitting me for using globals putting them into an object would either make it unreachable by other objects, or require adding excess arguments to method calls) for example
main.cpp
bool firstLoop;
const int dt = 10;
thing.cpp
void thing::Update(Object * thingToUpdate){
if( firstLoop){
...
} else{
// working with dt
}
}
but every time I try to access the variables I get a error of "undeclared identifier"
do I need to mark them as extern, or something else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
thing.cpp
中,写入注意,同样的技巧不适用于
dt
,因为它具有内部链接(因为它是内置类型的 const 变量)。通常,您应该有一个头文件,在其中声明(使用
extern
)需要从任何地方访问的变量,以及另一个源文件< strong>定义这些变量。内置类型的常量变量应在头文件中定义,或者应通过 selfsame extern 显式地将它们的链接设为外部 > 关键字。那么头文件应该包含在需要访问的任何地方。例子:In
thing.cpp
, writeNote that the same trick will not work for
dt
because it has internal linkage(because it's a const variable of built-in type).Usually, you should have one header file where you declare(with
extern
) your variables that need access from everywhere, and another source file which defines these variables. The constant variables of built-in type should be defined in the header file, or their linkage should be explicitly made external by means of the selfsame extern keyword. Then the header file should be included everywhere where access is needed. Example: