basic class issue

Started by
4 comments, last by bobofjoe 15 years, 12 months ago

basic_character::basic_character() //create a basic main character when called
{
	cout << "Characters Name: "; //print out message
		set_character_name(cin); //get from player characters name
	set_character_hp(100); //set the characters initial hp
}
</source>

hi, on the above code originally i had it like:
<source>
basic_character::basic_character() //create a basic main character when called
{
	cout << "Characters Name: "; //print out message
		set_character_name = define_character_name; //get from player characters name
	set_character_hp = 100; //set the characters initial hp
}
</source>

and it gave me errors
error C2065: 'define_character_name' : undeclared identifier
and another saying the operator
error C2659: '=' : function as left operand

do you see anything wrong? thanks.

btw - either way it didnt work
[Edited by - BlaDe16 on April 22, 2008 7:42:18 PM]
Advertisement
character.h
/*This header file creates the basic template model of all game characters*/#ifndef CHARACTER_H //if header not defined (created)...#define CHARACTER_H //define it...#include &lt;string&gt; // this allows the class to work with strings which are multiple characters like any 2 or more letter wordusing namespace std;class basic_character //basic creation of character with default stats{public: //public class means anything can access this class	basic_character(); //constructor, create new object in this class	~basic_character(); //destructor, destroy object created	string get_character_name() //return the characters name to the game	{		return character_name; //same as above comment	}; 	int get_character_hp() //return the characters hp to the game	{		return character_hp;  //same as above comment	}; 		string set_character_name(string define_character_name) //when a new character is born define their name is set here	{		character_name = define_character_name; //send defined name to protected: character_name	};		void set_character_hp(int initial_hp) //when a new character is born his initialhp is set here	{		character_hp = initial_hp; //send initial_hp to protected: character_hp	};protected: //means this class and classes derived from this class can access this 	string character_name; //characters name that is protected from the being accessedint character_hp; //characters hit pointsprivate: //means only this class can access this class};#endif //end of header (works with #ifndef)</source>
basic_character::basic_character() //create a basic main character when called{	cout &lt;&lt; "Characters Name: "; //print out message	cin &gt;&gt; character_name_temp; //get from player characters name and put it in a temp	new_character_attributes(character_name_temp,100); //send character name and initial hp over to the class}error C2065: 'character_name_temp' : undeclared identifieron lines 11 and 12

basic_character::basic_character() //create a basic main character when called{	cout << "Characters Name: "; //print out message	cin >> character_name_temp; //get from player characters name and put it in a temp	new_character_attributes(character_name_temp,100); //send character name and initial hp over to the class}error C2065: 'character_name_temp' : undeclared identifieron lines 11 and 12



You are using cin >> character_name_temp but character_name_temp isn't defined in your .h file. It should say:
cin >> character_name


protected: //means this class and classes derived from this class can access this 	string character_name; //characters name that is protected from the being accessedint character_hp; //characters hit pointsprivate: //means only this class can access this class



As you can see the variable name is character_name in your .h file.
thanks:character.cpp/*this file creates the character object and its initial parameters*/#include <iostream> //cin, cout, etc. (input/output stream)#include <string> // this allows the class to work with strings which are multiple characters like any 2 or more letter word#include "character.h" //classes for character creationusing namespace std; //use the standard name space, meaning cout instead of std::coutbasic_character::basic_character() //create a basic main character when called{	string character_name_temp;	cout << "Characters Name: "; //print out message	cin >> character_name_temp; //get from player characters name and put it in a temp	new_character_attributes(character_name_temp,100); //send character name and initial hp over to the class}main.cpp/*main game file*/#include <iostream> //cin, cout, etc. (input/output stream)#include "functions.h" //the standard functions i've made#include "character.h" //classes for character creationusing namespace std; //use the standard name space, meaning cout instead of std::coutint main() //the main function of the game, must be here and named main{	basic_character instance(<params>)//call character classPressAnyKey(); //press any key to close, causes a pause in game before exit		return 0; //tells main everything was okay and to exit out}character.h#include <string> // this allows the class to work with strings which are multiple characters like any 2 or more letter wordusing namespace std;class basic_character //basic creation of character with default stats{public: //public class means anything can access this class	basic_character(); //constructor, create new object in this class	~basic_character(); //destructor, destroy object created	string get_character_name() //return the characters name to the game	{		return character_name; //same as above comment	}; 	int get_character_hp() //return the characters hp to the game	{		return character_hp;  //same as above comment	}; 		void new_character_attributes(string define_character_name, int initial_hp) //when a new character is born define their attributes here	{		character_name = define_character_name; //send defined name to protected: character_name		character_hp = initial_hp; //send initial_hp to protected: character_hp	};protected: //means this class and classes derived from this class can access this 	string character_name; //characters name that is protected from being accessedint character_hp; //characters hit pointsprivate: //means only this class can access this class};#endif //end of header (works with #ifndef)
/*	character.h*/#include <iostream>#include <string>//using namespace std; This is a bad practice, since it pollutes the global namespac.class basic_character{public:	basic_character(); //Here is the constructor.	~basic_character();		//putting std:: before using stuff in std:: dosn't take too much time, and it makes it so much cleaer.	//We will not define the functinons in line, since that clutters the header file. Its easier to declare it in the .cpp	std::string get_character_name();	int get_character_hp();		//void new_character_attributes(string define_character_name, int initial_hp) This is what the constructor is for.	protected:	//The prefix character_ isn't really needed, since its implied that its part of the character class by being in the class already.	//We will leave that alone, for now.	std::string character_name;	int character_hp;};/*	character.cpp*/#include "character.h"basic_character::basic_character(){	//Make a temporary holder string.	std::string tempName;	std::cout<<"Character's Name: ";	//Get the name that we want.	std::cin>> tempName;		//Since you only call new_character_attribiutes once, we can just inline it here, ITs what the constructor is for, anyway.	character_name = tempName;	character_hp = 100;}std::string basic_character::get_character_name(){	return character_name;}int basic_character::get_character_hp(){	return character_hp;}/*	main.cpp*//*main game file*/#include <iostream> //cin, cout, etc. (input/output stream)#include "functions.h" //the standard functions i've made#include "character.h" //classes for character creationint main() //the main function of the game, must be here and named main{	basic_character instance; //Here we have an instance of basic_character.	std::cout<<instance.get_character_name() <<" has " << instance.get_character_hp() <<" Health.\n";		PressAnyKey(); //press any key to close, causes a pause in game before exit	return 0; //tells main everything was okay and to exit out}

[size=1]Visit my website, rawrrawr.com

This topic is closed to new replies.

Advertisement