I'm creating a text roleplaying game in c++, everything has gone quite good for a while now but i'm stuck right now. The problem is in the class "items". It's not the class itself but when I try to give every instance of the structures a value/values an error occur.
Here is the code:
class user {
class inventory {
public:
inventory() {
total_items=0;
used_items=0;
}
int total_items;
int used_items;
void add_item(int id, string name);
void use_item();
void show_items();
};
inventory inv;
public:
string name;
// Player attributes and skill levels
int hp;
int level;
int attack;
int life;
// The conclassor of the player class
user() {
hp=100;
level=1;
attack=1;
life=1;
}
void inventory() {
inv.show_items();
}
void pickup(int id, string item) {
inv.add_item(id, item);
}
// A function that shows the player's current stats
void stats() {
cout << endl << endl;
cout << "------------------------------- STATS --" << endl;
cout << "PLAYER: " << name << endl;
cout << "HP: " << hp << endl << endl;
cout << "LEVEL: " << level << endl;
cout << "ATTACK: " << attack << endl;
cout << "LIFE: " << life << endl << endl;
cout << "TOTAL ITEMS: " << inv.total_items << endl;
cout << "TOTAL ITEMS USED: " << inv.used_items << endl;
cout << "-- END STATS ---------------------------";
cout << endl << endl;
}
// The function that saves the current stats in the dat file
void save_stats() {
ofstream save;
save.open("save.dat");
for (int i=0;i<25;i++) {
cout << ".";
Sleep(30);
}
cout << endl << endl;
cout << "-- SAVE GAME ---------------------------" << endl;
if (!save) {
cout << "Could not save the game!" << endl;
}
string s_name = "PLAYER: ";
string s_hp = "HP: ";
string s_level = "LEVEL: ";
string s_attack = "ATTACK: ";
string s_life = "LIFE: ";
string s_items = "TOTAL ITEMS: ";
string s_used_items = "TOTAL ITEMS USED: ";
save << s_name;
save << name << endl;
save << s_hp;
save << hp << endl << endl;
save << s_level;
save << level << endl;
save << s_attack;
save << attack << endl;
save << s_life;
save << life << endl << endl;
save << s_items;
save << inv.total_items << endl;
save << s_used_items;
save << inv.used_items << endl << endl;
cout << "Game saved succefully.." << endl;
cout << "----------------------------------------";
cout << endl << endl;
save.close();
}
};
class items {
// ##################################
// Item list
// ######################
// Weapons
struct small_sword {
int id;
int damage;
int speed;
};
struct medium_sword {
int id;
int damage;
int speed;
};
struct power_sword {
int id;
int damage;
int speed;
};
struct flash_sword {
int id;
int damage;
int speed;
};
struct ultimate_sword {
int id;
int damage;
int speed;
};
// #######################
// ARMORS
struct small_shield {
int id;
int armor;
};
struct medium_shield {
int id;
int armor;
};
struct high_shield {
int id;
int armor;
};
struct mega_shield {
int id;
int armor;
};
struct ultimate_shield {
int id;
int armor;
};
// ######################
// General items
struct key {
int id;
};
struct healing {
int id;
};
struct full_healing {
int id;
};
// #################################
// End of item list
// ################################
// ITEM ATTRIBUTES
// ################################
// SWORDS
// id power speed
small_sword smallsword = {1, 2, 2};
medium_sword mediumsword = {2, 4, 3};
power_sword powersword = {3, 17, 2};
flash_sword flashsword = {4, 3, 16};
ultimate_sword ultimatesword = {5, 14, 11};
// ARMORS
// id armor
small_shield smallshield = {6, 2};
medium_shield mediumshield = {7, 4};
high_shield highshield = {8, 6};
mega_shield megashield = {9, 8};
ultimate_shield ultimateshield = {10, 10};
// GENERAL ITEMS
// id
key key = {11};
healing healing = {12};
full_healing fullhealing = {13};
// ################################
// END OF ITEM ATTRIBUTES
// ################################
};
For example:
small_shield smallshield = {6, 2);
If I edit it to:
small_shield smallshield;
without giving it a value, it works, if I edit every instance of the structures.
Here comes the error message, I will paste a part of it.
7 start.cpp
c:\documents and settings\xxx\desktop\game\classes.h:204: ANSI C++ forbids initialization of member `smallsword'
204 classes.h
making `smallsword' static
204 classes.h
in-class initialization of static data member of non-integral type `items::small_sword'
205 classes.h
ANSI C++ forbids initialization of member `mediumsword'
205 classes.h
making `mediumsword' static
205 classes.h
in-class initialization of static data member of non-integral type `items::medium_sword'
etc.....
This is really frustruating, mostly because it shouldn't be any error because I did the same thing, almost, in the user class on the top. Anyways there shouldn't be anything wrong.
That aside, what do you think of my code style, is it a good way to build a text based c++ game or does it just plain suck? took the opportunity to ask that while i'm at it :)
Thankful for help...
[Edited by - password on November 4, 2005 4:30:03 AM]