更新:我认为它是固定的。谢谢各位!
我搞错了,我就是搞不懂。我有这样的代码:
//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'我搜索了这个错误并查看了几个论坛帖子,但是每个人的问题似乎都不一样。我还尝试过搜索“类内的静态结构对象”和其他相关短语,但没有发现任何我认为与我的问题真正相关的短语。
我在这里要做的是:拥有一个我从未实例化过的类。并且在该类中有一个静态对象,该对象具有一个可以通过构造函数设置的变量。
任何帮助都是非常感谢的。谢谢。
发布于 2013-03-02 00:32:29
静态成员的定义不能在函数中:
class unInstantiatedClass{
public:
static Structure structObject;
};
Structure unInstantiatedClass::structObject(5);
int main() {
// Do whatever the program should do
}发布于 2013-03-02 00:32:55
我认为问题在于结构unInstantiatedClass::structObject(5);在main中。把它放在外面。
发布于 2013-03-02 00:28:30
您可能希望使用以下代码:
UPDATE:将静态成员的初始化移到全局范围。
// In global scope, initialize the static member
Structure unInstantiatedClass::structObject(5);
int main(){
return 0;
}https://stackoverflow.com/questions/15169169
复制相似问题