对类成员的访问“无效地使用非静态数据成员”
“Invalid use of non-static data member” for access of class member
我的代码有一个小问题。我试图创建一个简单的脚本与一个简单的头,但事情并没有完全解决。我得到以下错误:无效使用非静态数据成员'name'。真的很感谢一些帮助与这个问题,我仍然是一个初学者的c++。提前感谢!
//header file
#ifndef Game_main_h
#define Game_main_h
#include <iostream>
#include <string>
using namespace std;
class main
{
public:
void resetInput();
string name;
};
#endif
//executional file
#include "main.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <locale>
#include <sstream>
using namespace std;
int main(int argc, const char * argv[])
{
int nr;
string agree;
cout << "Enter your name.n";
std::getline(cin, main::name);
return 0;
}
您需要创建类main
的实例来访问它!例如:
main x;
std::getline(cin, x.name);
除此之外,把一个类命名为main
也不是个好主意。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 静态数据成员模板专用化的实例化点在哪里
- 调用在 HXX 文件中声明的静态数据成员
- 虚拟成员函数的定义是否强制在同一转换单元中动态初始化静态数据成员?
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 静态数据成员:它"const declaration / constexpr definition"起作用?
- 何时需要定义类的静态数据成员 (un/-)
- 为什么静态数据成员不能在c++11中的类中初始化
- 静态数据成员的模板专用化
- 拒绝包含某些公共静态数据成员的类型
- GCC:在调试构建中优化的静态数据成员
- 类模板静态数据成员定义/声明/初始化
- C++ 中的静态数据成员
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- 使用 lambda 函数初始化静态数据成员
- 引用静态数据成员
- 学习C++并在早期示例中遇到错误(在非静态数据成员之前需要构造函数)
- Windows 上的 Clang/LLVM 7 和 8 多次初始化内联静态数据成员(使用 link.exe 和 lld-
- 为什么不能使用"( )"为类的非静态数据成员提供默认值?
- 不带初始值设定项的 constexpr 静态数据成员