加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C:错误:无效使用qualified-name

发布时间:2020-12-16 10:23:53 所属栏目:百科 来源:网络整理
导读:更新:我认为这是固定的.多谢你们! 我收到了一个错误,我无法弄明白. 我有这个代码: //A Structure with one variable and a constructorstruct Structure{ public: int dataMember; Structure(int param);};//Implemented constructorStructure::Structure
更新:我认为这是固定的.多谢你们!

我收到了一个错误,我无法弄明白.
我有这个代码:

//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);”的行上,我收到一条错误,内容如下:

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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读