C++:错误:无效使用限定名

标签 c++ compiler-errors

更新:我认为它已修复。谢谢大家!

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

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

相关文章:

c++ - 从大括号 std::string 构造 std::string_view,clang 和 gcc 不同意 -Wconversion

java - Java代码pls错误

Swift Xcode 6.1 破坏了我的代码 |操作数错误

c++ - G++ 版本和标准选项

c++ - 如果同一数据位于不同的数据结构中,则维护它们的两个拷贝是否是一种不好的做法?

c++ - 从 C 转换为 C++ 后对共享库函数的 undefined reference

c# - 项目无法编译

c++ - 我可以从内存流加载库吗?

php - 尝试获取非对象 Eloquent 关系的属性

scala - Scala : Compilation error: Cannot resolve overloaded method