Jump to content
  • Advertisement
Sign in to follow this  
BlaDe16

basic class issue

This topic is 3740 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

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]

Share this post


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

using 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 accessed
int character_hp; //characters hit points

private: //means only this class can access this class
};

#endif //end of header (works with #ifndef)
</source>

Share this post


Link to post
Share on other sites

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 identifier
on lines 11 and 12

Share this post


Link to post
Share on other sites


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 identifier
on 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 accessed
int character_hp; //characters hit points

private: //means only this class can access this class




As you can see the variable name is character_name in your .h file.

Share this post


Link to post
Share on other sites

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 creation

using namespace std; //use the standard name space, meaning cout instead of std::cout

basic_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 creation

using namespace std; //use the standard name space, meaning cout instead of std::cout

int main() //the main function of the game, must be here and named main
{
basic_character instance(<params>)//call character class

PressAnyKey(); //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 word

using 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 accessed
int character_hp; //characters hit points

private: //means only this class can access this class
};

#endif //end of header (works with #ifndef)


Share this post


Link to post
Share on other sites

/*
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 creation

int 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
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!