对类成员的访问“无效地使用非静态数据成员”

“Invalid use of non-static data member” for access of class member

本文关键字:静态 数据成员 无效 成员 访问      更新时间:2023-10-16

我的代码有一个小问题。我试图创建一个简单的脚本与一个简单的头,但事情并没有完全解决。我得到以下错误:无效使用非静态数据成员'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也不是个好主意。