now I am working with c + +. So I'm even a small little program written Code: #include <iostream.h> #include <string> class Spieler { public: Spieler() { name = new char[]; } char *name; }; int main() { Spieler player1; cout << "Bitte Namen eingeben\n"; cin >> player1.name; cout << player1.name; return 0; } works, too. Only if I enter too many characters, gives irgendso ne bad message. Is there ne better?
I've already tried, but then reports the following: C: \ Program Files \ Microsoft Visual Studio \ MyProjects \ Classes \ klassenmain.cpp (17): 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 <ch ar>> '(or there is no acceptable conversion) C: \ Program Files \ Microsoft Visual Studio \ MyProjects \ Classes \ klassenmain.cpp (18): 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 <ch ar>> '(or there is no acceptable conversion)
Code: name = new char []; This is cheese. You must specify a size already. The version with std:: string is much better. To your problem: Make the <iostream.h> a <iostream> and you should be rid of the problem.
Ok, thank you for the quick answers ... But I Could you perhaps explain a few things: My code now looks like this: Code: #include <iostream> #include <string> using namespace std; class Spieler { public: std::string name; }; int main() { Spieler player1; cout << "Bitte Namen eingeben\n"; player1.name = "HALLO"; cout << player1.name; return 0; } What is the purpose of using namespace std ?????
Hi, if you with "new char [x]" want to leave it, then you still lack a "delete [] name" in dtor of player.