Oct 18, 2012 at 12:34am Oct 18, 2012 at 12:34am UTC
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "Test.h"
// Only instance of the Test class we allow
Test *Test::instance = NULL;
// Must be called in order to use the Test methods
Test *Test::get_instance() {
if (Test::instance == NULL) {
instance = new Test;
}
return instance;
}
void Test::test_item_class(void )
{
// Item constructor test
Item test_sword("sword" , Item::WEAPON, 10, 0, 0);
assert("sword" == test_sword.getName());
assert(Item::WEAPON == test_sword.getType());
assert(10 == test_sword.getPower());
assert(0 == test_sword.getVitality());
assert(0 == test_sword.getHealth());
assert(true == test_sword.mAvailable);
// Item operator= test
Item another_sword = test_sword;
assert("sword" == another_sword.getName());
assert(Item::WEAPON == another_sword.getType());
assert(10 == another_sword.getPower());
assert(0 == another_sword.getVitality());
assert(0 == another_sword.getHealth());
assert(true == another_sword.mAvailable);
// The weapons shouldn't share the same memory address.
assert(&another_sword != &test_sword);
// Item void constructor test
Item void_sword;
assert("" == void_sword.getName());
assert(Item::WEAPON == void_sword.getType());
assert(0 == void_sword.getPower());
assert(0 == void_sword.getVitality());
assert(0 == void_sword.getHealth());
assert(true == void_sword.mAvailable);
// Item setters and getters test
void_sword.setPower(99);
void_sword.setName("steel sword" );
void_sword.setVitality(100);
void_sword.setType(Item::ARMOR);
void_sword.setHealth(36);
void_sword.mAvailable = false ;
assert("steel sword" == void_sword.getName());
assert(Item::ARMOR == void_sword.getType());
assert(99 == void_sword.getPower());
assert(100 == void_sword.getVitality());
assert(36 == void_sword.getHealth());
assert(false == void_sword.mAvailable);
}
Item.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
class Item {
public :
enum TYPE { WEAPON, ARMOR, HEALTH };
Item( string name, Item::TYPE type, int power, int vitality, int health );
Item operator = ( Item& newItem );
Item( void );
// ~Item()
void setPower( int value );
int getPower( void );
void setVitality ( int value );
int getVitality ( void );
void setHealth( int value );
int getHealth( void );
void setName( string name );
void getName( void );
void setType( TYPE newType );
TYPE getType( void );
string mName;
int mPower;
int mVitality;
int mHealth;
TYPE mType;
bool mAvailable;
};
Last edited on Oct 18, 2012 at 12:58am Oct 18, 2012 at 12:58am UTC
Oct 18, 2012 at 12:38am Oct 18, 2012 at 12:38am UTC
Seems like getName() doesn't return the name actually.
Oct 18, 2012 at 12:41am Oct 18, 2012 at 12:41am UTC
Hmmm..how would I fix this issue as far as that? I must retrieve the name!
Last edited on Oct 18, 2012 at 12:46am Oct 18, 2012 at 12:46am UTC
Oct 18, 2012 at 12:52am Oct 18, 2012 at 12:52am UTC
No idea since you haven't shown us the constructor for Item or the getName function.
Oct 18, 2012 at 12:58am Oct 18, 2012 at 12:58am UTC
I apologize, added the full class.
Oct 18, 2012 at 1:05am Oct 18, 2012 at 1:05am UTC
What is labeled are Item.cpp is the .h file.
One obvious error I see in the Item class declaration is that getName() returns type void. Shouldn't it return a string?
If that's not your problem, please post the implementation of the Item constructor and the implementation of the get_name function. No need to post the entire Item.cpp file.