警告:这很可能是一个非常愚蠢的问题。
我有一个对象驻留在它自己的头文件中并在 main 中创建,我想使用在单独文件中声明的函数更新所述对象。如果我不清楚,请告诉我,我会尝试更好地解释自己。
//main
#include"Object.h"
int main(){
Object obj;
}
假设上面是main,创建了obj。
//functions file which is separate from both the main and object.h
void updateObj(int someNum){
//how can I do this??
obj.callingObject(someNum);
}
谢谢
最佳答案
你在main中声明的Object是在main的范围内。因此“updateObj”无法访问它。以下之一将使这项工作:
// This (IMO) is a wrong way. Unless perhaps obj is a singleton and then you should just make it such.
//main
Object obj;
int main()
{
}
// functions file
extern Object obj;
void updateObj(int someNum)
{
obj.callingObject(somenum)
}
另一种方式是写:
void updateObj(int somnum, Object& obj)
{
}
关于c++ - 类和更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3187551/