更新:我认为它已经修复了。多谢你们!
我收到错误,但我无法弄清楚。
我有这个代码:
//A Structure with one variable and a constructor
struct Structure{
public:
int dataMember;
Structure(int param);
};
//Implemented constructor
Structure::Structure(int param){
dataMember = param;
}
//A class (which I don't plan to instantiate),
//that has a static object made from Structure inside of it
class unInstantiatedClass{
public:
static Structure structObject;
};
//main(), where I try to put the implementation of the Structure from my class,
//by calling its constructor
int main(){
//error on the line below
Structure unInstantiatedClass::structObject(5);
return 0;
}
在“Structure unInstantiatedClass::structObject(5);”行上,我收到一条错误消息:
error: invalid use of qualified-name 'unInstantiatedClass::structObject'
我用谷歌搜索了这个错误并浏览了几个论坛帖子,但每个人的问题似乎都不同。我还尝试过谷歌搜索“类内的静态结构对象”和其他相关短语,但没有找到任何我认为与我的问题真正相关的短语。
我在这里想做的是:
有一个我从未实例化的类。在该类中拥有一个静态对象,该对象具有一个可以通过构造函数设置的变量。
任何帮助表示赞赏。谢谢。
静态成员的定义不能位于函数内部:
class unInstantiatedClass{
public:
static Structure structObject;
};
Structure unInstantiatedClass::structObject(5);
int main() {
// Do whatever the program should do
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)