Using String Variable to Create Class Instances

I am trying to feed a string variable into the Class Constructor the data will be coming from cin. How might I do this. This is the function I am trying to us it in. and the Class is called Player, constructor is Player::Player of course. The Constructor works great I am just trying to figure out how to get input from user to name the class instance.



void makeCharacter()
{
system("cls");
string name;
cout << "What is your characters name?" << endl;
getline(cin, name);
Player name;
}
Don't use system("cls"); http://www.cplusplus.com/forum/articles/10515/ http://www.cplusplus.com/forum/articles/11153/
Use [code][/code] tags when posting code

In the line Player name; you are declaring a variable called 'name' of type 'Player' but 'name' was already declared as string.
To create an object of class 'Player' passing 'name' to the constructor you should do something like these:
1
2
Player a_player ( name ); // explicit call of the constructor
Player another_player = name; // implicit call of the constructor 
Notice that the scope would of the Player object you create in the function would be over when the function ends so you may return a copy of it:
1
2
3
4
5
Player /*Notice: not void*/ makeCharacter()
{
    // etc.
    return Player ( name ) ; // just call the constructor and return the object it created
}
Last edited on

Ok so would this work. I am just doing this for fun and to learn but I am trying to set it up so I can run multi "Player" class objects with differnet names. which gets there names form the command imput. also are you saying that if I want the Object to remain it needs to be called directly fomr the main() Function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Player::Player()
	{
		cout << "What is your name?" << endl;
		getline(cin, c_p_name);
		m_p_name = c_p_name;

		cout << "\nWhat is your Characters name?" << endl;
		getline(cin,c_name);
		m_name = c_name;

		cout << "\nWhat Race do you desire?" << endl;
		getline(cin,c_race);
		m_race = c_race;

		cout << "\nWhat Class do you desire?" << endl;
		getline(cin,c_class);
		m_class = c_class;

		cout << "\nHow old is your character?" << endl;
		cin>>c_age;
		m_age = c_age;

		//system("cls"); OK i know I shouuld not use this
		cout << "\n Player :\t\t" << m_p_name;
		cout << "\n Character :\t\t" << m_name;
		cout << "\n Race : \t\t" << m_race;
		cout << "\n Class : \t\t" << m_class;
		cout << "\n Age : \t\t\t" << m_age << endl;

		Return m_name;
	}
Last edited on
You could avoid having all the local variables my simply doing getline(cin, m_p_name); //etc

Also, constructors don't return anything.

If you want that code to run every time you create a Player object, then yeah, that would work (after you fix the above errors).
LOL yeah I did not even think about the fact that the constructor has access tot he private part of class. that does make my coding like double what it needs to be. thankx
Never get user input directly in the constructor.
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct BadClass
{
     string name;

     BadClass()
     {
           cout << "Give my name plz ";
           getline ( cin, name );
     }
};

struct goodClass
{
     string name;
     goodClass ( string Name ) : name ( Name ) // equivalent to name = Name but this would call name's constructor, not = operator
     {
     }
};

int main()
{
    BadClass foo;

    string bar_name;
    cout << "Give my name plz ";
    getline ( cin, bar_name );
    goodClass bar ( bar_name );
   
}


You should give each object the scope you need. If you need it to be visible in main, declare it in main
So I should use Paramiters for my Contructor then somthing like Player::Player(string m_pname, string m_name, string m_class, int m_age) Allthough I guess I dont need to Declare these since they are in the class private
1
2
3
4
5
6
private:
	string m_pname;
	string m_name;
	string m_class;
	string m_race;
	int m_age;


Like I said I am just doing this for fun to learn so pretty much any and all suggestions help. Thankx
Last edited on
Declare the parameters with different names from the class members and then use them to initialize the class members
I find no reason to name things differently, except as a hint to myself that certain members are private. I expect the names I use in my class's declaration to be documentary, not obfuscatory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Foo
  {
  public:
    string   name;
    unsigned age;
    bool     has_job;

    Foo( const string& name, unsigned age, bool has_job ):
      name( name ),
      age( age ),
      f_can_vote( age >= 18 )
      {
      this->has_job = has_job;
      }

  private:
    bool f_can_vote;
  };

Hope this helps.
Topic archived. No new replies allowed.