hmmmm
why does that compile (after removing
#inculde <conio.h> and
getch(); (conio.h is a dos lib that doesnt exist in linux... interestingly enough i was planing on implementing ncurses, which is a fork of a port i believe, somewhere along the way) and run without problem.....
its doing practically the same thing.
...
hahaha
so i go to post a cut down version of what im doing, then think i may as well make it compilable. so put in some of the implementation i had taken out in as short as i can (in class definitions for the most part) and then compile it.
sall good.
i run it and dont have any problems.
well, at least im amused by it as opposed to extremely frustrated.
in any case, here is the cut down version of my program.
the destructors are commented cause i didnt provide implementation for them.
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <list>
#include <cstring>
const int MAX_NAME_LENGTH=20; //or something
const int MAX_DSC_LENGTH=50; //or something
enum thingtype {THING, THINGWITHSTUFF, ITEM, ROOM, CHARACTER};
class thing{
public:
// constructor
thing();
thing(const char *name);
// thing(const thing &c);
// destructor
// virtual ~thing();
//some other stuff
//m_Names ref funcs
void setName(const char *name);
const char *getName(void) const;
//m_Disc ref funcs
void setDisc(const char *disc);
const char *getDisc(void) const;
//some other stuff
protected:
char m_Name[MAX_NAME_LENGTH];
char m_Disc[MAX_DSC_LENGTH];
// some other stuff
thingtype ThingType;
};
thing::thing(){
ThingType=THING;
}
thing::thing(const char *name){
setName(name);
thing();
}
void thing::setName(const char *name){
strcpy(m_Name, name);
}
const char *thing::getName(void) const{
return m_Name;
}
void thing::setDisc(const char *disc){
strcpy(m_Disc, disc);
}
const char *thing::getDisc(void) const{
return m_Disc;
}
class item : public thing {
public:
//constructors
item(){ThingType=ITEM;}
item(const char *name):thing(name){item();}
};
typedef std::list<item*> inventory;
class thingWithStuff: public thing{
public:
//constructors
thingWithStuff(){ThingType=THINGWITHSTUFF;}
thingWithStuff(const char *name):thing(name){thingWithStuff();};
// virtual ~thingWithStuff();
//other stuff (actually another list, but not one i had started to test. ill cross
//that bridge when i come to it(which should be easer cause im implementing it the
//same way i am the one that currently causing problems (its a list of a third class
//derived from thing (i sware there is a method to my madness))))
private:
//inventory inventory;
inventory inv;
};
class room : public thingWithStuff {
public:
//constructor
room();
room(const char *name);
//destructor
// virtual ~room();
//and our addChar class that seems to be where we are getting all our problems
//add character to the list
void addChar(thingWithStuff* _Char);
private:
room *ptr_North;
room *ptr_South;
room *ptr_East;
room *ptr_West;
// i seem to remember that being protected instead of private. no matter
// my list, declared exactly the same way you have yours.
std::list<thingWithStuff*> charList;
};
//rooms default constructor. notice all the things ive tried to do in it
room::room(){
ptr_North=0;
ptr_South=0;
ptr_East=0;
ptr_West=0;
ThingType=ROOM;
// std::list<thingWithStuff*> l;
// charList = std::list<thingWithStuff*>::list();
//charList.list();
//thingWithStuff *lol;
// charList.push_back(lol);
// charList.assign(1, lol);
}
//the constructor that im calling, which happens to call the default one anyway.
//if you had done something with your constructor then i would have thought that
//the problem lay here...
room::room(const char *name):thingWithStuff(name){
room();
}
//implementation for addChar()
//i use printf instead of cout, but for all intents and purposes they are the same.
//it seg faults on the push_back() line.
//the commented line has the dynamic_cast you asked about i believe. either line works.
void room::addChar(thingWithStuff* _Char){
printf("putting %s in %s.\n",_Char->getName(),getName());
this->charList.push_back(_Char);
// this->charList.push_back(dynamic_cast<thingWithStuff*>(_Char));
}
class character : public thingWithStuff{
public:
//constructors
character(){ThingType=CHARACTER;}
character(const char *name):thingWithStuff(name){character();}
//destructor
// virtual ~character();
//some other stuff
};
int main(){
room* myRoom = new room("My Room");
myRoom->setDisc("a test room");
character *player = new character("Tester");
myRoom->addChar(player);
delete myRoom;
delete player;
puts("ZOMG I GOT TO THE END");
return 0;
}
|
well that's brilliant. now i have absolutely no idea what's going on.
EDIT:
ok, so im guessing that im doing something in a bad way somewhere and its dieing.
perhaps my strings. i have a few others for discriptions.
i am also sporting a second (or first) list of
items* (a class derived from thing)
the rooms have pointers to other rooms. the character has a pointer to a room.
could these cause problems?
EDIT 2: just tested the strings twice. dont think thats it.
EDIT 3: and now ive added in my second list... still no seg fault
EDIT 4: and it turns out im doing bad things with c style strings. cause i had fixed length strings i was causing a buffer overflow.
thanks everyone for the help.