Jump to content
  • Advertisement
Sign in to follow this  
hereticprophecy

Trouble with member functions

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

If you intended to correct an error in the post then please contact us.

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;

		cout << "Please enter your character's first name.\n";
		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 << "Please choose your race.\n";
		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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!