سؤال

I had a little problem with my code. I am trying to create a simple script with a simple header, but things aren't quite working out. I get the following error: Invalid use of non-static data member 'name'. Would really appreciate some help with this problem, I am still a beginner to C++. Thank you in advance!

//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;
}
هل كانت مفيدة؟

المحلول

You need to create an instance of class main to access it! E.g. say

main x;
std::getline(cin, x.name);

Besides that it's a not so good idea to name a class main.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top