更新:我认为它已修复。谢谢大家!
我收到一个错误,我无法弄清楚。 我有这段代码:
//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
}
关于C++:错误:无效使用限定名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15169169/