C ++:错误:无效使用qualified-name [英] C++: error: invalid use of qualified-name

查看:3593
本文介绍了C ++:错误:无效使用qualified-name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我认为它是固定的。非常感谢!

我收到一个错误,我只是无法弄清楚。
我有这个代码:

I'm getting an error, and I just can't figure it out. I have this code:

//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;
}



在结构unInstantiatedClass :: structObject(5) ,我得到一个错误,读为:

On the line that says "Structure unInstantiatedClass::structObject(5);", I get an error that reads:

error: invalid use of qualified-name 'unInstantiatedClass::structObject'

我已经google了这个错误,通过几个论坛帖子,但每个人的问题似乎不同。我也试过googling静态结构对象里面的类和其他相关的短语,但没有发现任何我认为真正与我的问题相关。

I've googled this error and looked through several forum posts, but everybody's problems seem to be different. I've also tried googling "static struct object inside class" and other related phrases, but haven't found any that I think really relate to my problem.

在这里要做的是:
有一个类,我不曾实例化。和在类中有一个静态对象,它有一个可以通过构造函数设置的变量。

What I'm trying to do here is: Have a class which I don't ever instantiate. and Have, inside that class, a static object, which has a variable that can be set via constructor.

任何帮助是赞赏。感谢。

Any help is appreciated. Thanks.

推荐答案

静态成员的定义不能在函数内:

The definition of the static member can't be inside a function:

class unInstantiatedClass{
  public:   
    static Structure structObject;
};

Structure unInstantiatedClass::structObject(5);

int main() {
    // Do whatever the program should do
}

这篇关于C ++:错误:无效使用qualified-name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆