- Oct 21, 2003
 
- 99
 
- 0
 
- 0
 
Im trying to create a class which has a string data member, and in the constructor, the user is asked to input their name and the name is stored in that data member. I tried substituting "string" with char*. That one compiles, but crashes after the user presses enters their name, and presses ENTER... Here is the code I have so far... (Compiled with Visual C++ 6.0) 
#include <iostream>
using namespace std;
class Player
{
public:
Player ( ) ;
string GetPlayerName ( ) ;
 
private:
string name;
};
Player:
layer ( )
{
cout << "What is your name? " ;
cin.getline(name, 20) ; // <--- line 20
}
string Player::GetPlayerName ( )
{
return name;
}
int main ( )
{
Player theplayer;
cout << theplayer.GetPlayerName(); // <--- line 32
return 0;
}
Here are the error messages I get (the lines where the errors occur, are noted above with comments):
C:\programs\class2\class2.cpp(20) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' : cannot convert parameter 1 from 'class std::b
asic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\programs\class2\class2.cpp(32) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversi
on)
I can't see for the life of me why I cant enter keyboard input into a "string" variable, or why I can't pass a string variable to "cout"...
			
			#include <iostream>
using namespace std;
class Player
{
public:
Player ( ) ;
string GetPlayerName ( ) ;
private:
string name;
};
Player:
{
cout << "What is your name? " ;
cin.getline(name, 20) ; // <--- line 20
}
string Player::GetPlayerName ( )
{
return name;
}
int main ( )
{
Player theplayer;
cout << theplayer.GetPlayerName(); // <--- line 32
return 0;
}
Here are the error messages I get (the lines where the errors occur, are noted above with comments):
C:\programs\class2\class2.cpp(20) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' : cannot convert parameter 1 from 'class std::b
asic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\programs\class2\class2.cpp(32) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversi
on)
I can't see for the life of me why I cant enter keyboard input into a "string" variable, or why I can't pass a string variable to "cout"...
				
		
			