Original Post
Okay, the below program is an experiment in using classes. I create a class named CPlayer and give it a few data members and methods. I also create an operator, in atempt to learn how to use operators correctly, and that part doesn't work. (Evidently my book didn't cover it well enough!) The operator should sum the values of the level data member and reasign it to the current class object. Currently my program gives an illegal operation error which the debugger identifies as an "Expression: _CrtlsValidHeapPointer(pUserData)
Heres the code I'm using:
Hope someone can see something wrong with this, I followed my book to a tee.
I changed the operator function prototype and definition to return a type "CPlayer &" instead of just "CPlayer" and then changed the operator definition code to:
It now adds the levels correctly as it should. Why doesn't the first way work though, if anyone knows this, I would appreciate hearing, so I can know not to make that mistake.
-
GamerAlan
[edited by - GamerAlan on February 22, 2003 2:18:58 AM]
#include <iostream>
#include <string.h>
class CPlayer //creating the class
{
private:
char *name;
unsigned short level; //the problem member data
unsigned short strength;
public:
CPlayer(void);
CPlayer(unsigned short lvl, unsigned short str);
CPlayer(CPlayer & copyPlayer);
~CPlayer(void);
CPlayer operator + (CPlayer & otherPlayer); //the operator that doesn't want to work
char * GetName();
void LevelUp();
int GetLevel();
void Fight();
};
CPlayer::CPlayer(void)
{
level = 0;
}
CPlayer::CPlayer(unsigned short lvl, unsigned short str)
{
level = lvl;
strength = str;
std::cout << "Enter your character name: ";
char tempName[1024];
std::cin >> tempName;
int nameLength = strlen(tempName);
name = new char[nameLength + 1];
strcpy(name, tempName);
}
CPlayer::CPlayer(CPlayer & copyPlayer)
{
level = copyPlayer.level;
strength = copyPlayer.strength;
//char tempName[1024] = copyPlayer.GetName();
char tempName[1024];
strcpy(tempName, copyPlayer.GetName());
int nameLength = strlen(tempName);
name = new char[nameLength + 1];
strcpy(name, tempName);
}
CPlayer::~CPlayer(void)
{
if (name != NULL)
{
delete [] name;
}
}
CPlayer CPlayer::operator + (CPlayer & otherPlayer) //that operator being defined...
{
CPlayer tempPlayer;
tempPlayer.level = level + otherPlayer.GetLevel();
return tempPlayer;
}
char * CPlayer::GetName()
{
return name;
}
void CPlayer::LevelUp()
{
level += 1;
std::cout << "Duh do duh doo doo doo! Level up!" << std::endl;
}
int CPlayer::GetLevel()
{
return level;
}
void CPlayer::Fight()
{
std::cout << "Using a strength of " << strength << " ";
std::cout << name << " kills the monster!" << std::endl;
}
int main()
{
CPlayer player1(1,5);
std::cout << "Your character is named: " << player1.GetName() << "\n" << std::endl;
std::cout << "Here comes a monster!\n" << std::endl;
player1.Fight();
std::cout << "Good job!" << std::endl;
player1.LevelUp();
std::cout << "Now your level is: " << player1.GetLevel() << "!\n" << std::endl;
std::cout << "See ya later!" << std::endl;
CPlayer player2(player1);
std::cout << "Welcome player2: " << player2.GetName() << std::endl;
std::cout << "Player 2 level: " << player2.GetLevel() << std::endl;
player2 = player2 + player2; //programs use of the operator
std::cout << "Player 2 level now: " << player2.GetLevel() << std::endl;
return 0;
} CPlayer & CPlayer::operator + (CPlayer & otherPlayer)
{
level = level + otherPlayer.GetLevel();
return *this;
}