Darryn Zartman
Darryn Zartman

Reputation: 13

std::string’ has no member named ‘get_name'

I keep getting this error,

  error: ‘std::string’ has no member named ‘get_name’
       cout << name.get_name() << name.get_won() << name.get_lost << endl << endl;

even though 'name = name_of_player;' and 'cout << name;' prints the correct name. I am guessing that it is taking the variable and not the actual string? I am not entirely sure, thus I need help.

#include <iostream>
#include <string>

using namespace std;

class person {

   string name_;
   int won_;
   int lost_;

   public:
   void set_name(string);
   void set_wl(int,int);
   int get_won() const {return won_;}
   int get_lost() const { return lost_;}
   string get_name() const {return name_;}
};

void person :: set_name(string n) {
   name_ = n;
}

void person :: set_wl(int x, int y) {
   won_ = x; lost_ = y;
}

int number_of_players;
string name;
string name_of_player[31];
int counter;

void get_player()
{
   counter = 1; //Initiating the counter
   cout << endl << "Ok! Now we are going to enter their names in. " << endl;
   for (int i=1; number_of_players >= i; i++)
   {
      cout << "Enter player #" << counter << "'s name... " << endl;
      cout << "The max is 30 players... " << endl;
      getline (cin, name);
      cout << endl;
      cout << "Okay, so player #" << counter << " is " << name << "." << endl;
      cout << "Please press Enter to continue..." << endl;
      cin.ignore();
      name_of_player[counter] = name;
      person name;
      name.set_name(name_of_player[counter]);
      name.set_wl(0,0);
      counter = (counter + 1);
   }

}

void list_players()
{
   counter = 1; //initiating the counter
   cout << "Here are the current players with their respective win/loss ration... " << endl << endl;
   for (int i=1; number_of_players >= i; i++)
   {
      name = name_of_player[counter];
      cout << name.get_name() << name.get_won() << name.get_lost << endl << endl;

      counter = counter +1;
   }
}

int main()
{
   cout << "Welcome to the Chess Tournament Organizer. " << endl;
   cout << "How many people will be playing today? " << endl;
   cin >> number_of_players;
   cin.ignore();
   get_player();
   list_players();
   return (0);
} 

******************************************************************************

EDIT: I have solved it and figured it out, past all of my terrible code. The problem was, for every name, I was creating a class of 'person' with that name. But this was bad because it was a local scope. So what I did was change the global string 'name_of_players' to a global variable of the person class with

person name_of_player[31];

Changing also...

cout << "Enter player #" << counter << "'s name... " << endl;
    cout << "The max is 30 players... " << endl;
    getline (cin, name);
    cout << endl;
    cout << "Okay, so player #" << counter << " is " << name << "." << endl;
    cout << "Please press Enter to continue..." << endl;
    cin.ignore();
    name_of_player[counter].set_name(name);
    name_of_player[counter].set_wl(0,0);

And doing the same to the 'list_of_players' function.

I am still not completely sure why it didn't work, I wanted several 'person's identified by their names, created by using the name given. So if a user were to input "mike" there would be a person class called mike, and I could find the information of the person class mike, by inputing mike.get_info();

TL;DR I needed to fix the scope and not run my string as a name of the person class for each person.

Upvotes: 1

Views: 3371

Answers (3)

Jonathan Mee
Jonathan Mee

Reputation: 38939

name_of_player is defined as an array of 31 strings:

string name_of_player[31];

You get the element from that array enumerated by counter when you do:

name = name_of_player[counter]; 

So name is a string. strings do not have a get_name method.

You may be confused about what name is cause you define a local person name in get_player, but then you never use that locally defined name.


An ugly way to solve this would be to create another global where you declare name_of_player as:

person stats_of_player[31];

Then at the bottom of the for-loop in get_player you can assign the locally declared variable to the global array:

stats_of_player[counter] = name;

You would then be able to use stats_of_player in list_players instead of your for-loop as follows:

for (int i=1; number_of_players >= i; i++)
{
    cout << stats_of_player[i].get_name() << stats_of_player[i].get_won() << stats_of_player[i].get_lost() << endl << endl;
}

Upvotes: 3

ZachSand
ZachSand

Reputation: 163

Running your code I get that std::string has no member named get_name, get_won, or get_lost. This is because you are trying to invoke those methods on a c++ string object, which does not have those methods.

Furthermore it seems like you expect name_of_player to be an array of players when it is in fact an array of strings.

Because the methods of your person class are public and not static you need an instance of your person class to invoke those methods (outside of the class definition e.g. main).

void get_player() {
  ....
  player p();
  p.set_name("Bob");
  p.get_name();
  ...
}

Upvotes: 0

edmarisov
edmarisov

Reputation: 148

name_of_player is array of strings, not person objects. So you trying to call get_name on string object and this is why you are getting such error.

Upvotes: 0

Related Questions