Public Group

# Trouble with member functions

This topic is 4281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Question: Is <iostream> unavailable when declaring the members of a class? Today has been my first day working with classes, and until a few minutes ago, it was going great. After declaring a few classes and fiddling with values, constructors, instantialization, and pointers, I attempted to make a master class that used another class as a member. Here's where it got hairy: all have worked great except one, the class holding information on each possible race for the character in question.
class Character {
string First_Name, Surname, Player_Name;
Base_Stat Str, Dex, Con, Int, Wis, Cha;
Race Race_;
public:
Character ();
void Generator () {
int a;

cin >> First_Name;
cout << "Until your character earns one, he/she will have no surname.\n";
cout << "What is they player's name?\n";
cin >> Player_Name;
cout << "1.)Human\n2.)Elf\n3.)Dwarf\n4.)Halfling\n5.)Gnome\n6.)Half-Orc\n7.)Half-Elf\n";

switch (a) {
case 1: {
Race_ = Human;
break;
}
case 2: {
Race_ = Elf;
break;
}
case 3: {
Race_ = Dwarf;
break;
}
case 4: {
Race_ = Halfling;
break;
}
case 5: {
Race_ = Gnome;
break;
}
case 6: {
Race_ = Half_Orc;
break;
}
case 7: {
Race_ = Half_Elf;
break;
}
}
}
};
Character::Character() {
First_Name = " ";
Surname = " ";
Player_Name = " ";
};

...is my declaration and construct for the Character's primary class, along with the function that's been bothering me, void Generator(). During compilation, I get two very long errors... error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion), and both correspond to the standard inputs I have for customizing the character's name and the player's name. I'm gonna continue to fiddle and see what I can figure out, but any insight would be greatly appreciated: I'm new to classes and I could very well be doing something stupid.

##### Share on other sites
Did you #include<string> as well ?

##### Share on other sites
Additionally - purely on stylistic and practical grounds, consider moving Generator outside the class, as you have with the constructor. Putting functions in the class body like that is rarely done in C++ unless it's a template or a very short inline function.

##### Share on other sites
Checklist:

1) have you #included <iostream>?
2) have you #included <string>?
3) have you specified using namespace std?

On (3)... as a general guideline, using directives in headers are bad practice, because it can affects files that #include the header without you realising it, which can lead to subtle bugs with unintended consequences. So, assuming this is a header file (I guess it might be a CPP file), it would be better to change that third point to:

3) have you specified std::cout, std::cin, and std::string?

I must admit, at first I thought it was this that was causing it; but the error message clearly states std::string. So I'm going to guess it's number 1 that's the fault. Certainly, the principle of what you are attempting to do is fine.

##### Share on other sites
When I said it was possible I was doing something stupid, I didn't realize I could actually miss #include <string>. Can't say I've ever made a mistake like that in the 3 years I've been programming. Thanks for the help, she's running beautifully now.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 12
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632929
• Total Posts
3009285
• ### Who's Online (See full list)

There are no registered users currently online

×