C++ Crash, Don't Know How to Fix

I'm taking a game dev class.
I kinda got ahead of the class and have separate classes for my game.

I had it working at one point, and now I can't figure out how to fix the problem.

I couldn't fit all of my code in here but when it crashes it opens the "Makefile" file in the Dec C++ program and highlights --

"$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)"

The report says this (http://gyazo.com/e51420887a7027c3f3893ac069f87974 )

I'll post some code in some replies since I couldn't fit it in here.

My different classes are -
main.cpp
enemy.cpp
weapon.cpp
armor.cpp
material.cpp (This isn't in use at the moment.)
Last edited on
Here's the main.cpp. The cout and pointless parts have been replaced with "\\..//"

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>
#include <windows.h>
#include "enemy.cpp"
#include "weapon.cpp"
#include "armor.cpp"

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

enemy e;
weapon w;
armor a;

int main(int argc, char** argv) {
	
	bool game;
	
	string playerName;
	string questGiver = "Verid";
	int numItems = 0;
	int maxItems = 10;
	string inv[maxItems];
	unsigned int money = 0;
	unsigned int baseHealth = 20;
	
	\\..//
	
	do {
		\\..//
		cout << "Which weapon do you want?\n";
		cout << "1. ";
		w.getWeapon();
		cout << "\n";
		
		cout << "2. ";
		w.getWeapon();
		cout << "\n";
		
		cout << "3. ";
		w.getWeapon();
		cout << "\n";
		
		int choose1;
		cin >> choose1;
		
		if(choose1 == 1){
			inv[numItems++] = w.getWeapon();
			cout << "\n";
			w.getWeapon(); 
			cout << " was added to your inventory.\n";
		}
		
		else if(choose1 == 2){
			inv[numItems++] = w.getWeapon();
			cout << "\n";
			w.getWeapon();
			cout << " was added to your inventory.\n";
		}
		
		else if(choose1 == 3){
			inv[numItems++] = w.getWeapon();
			cout << "\n";
			w.getWeapon();
			cout << " was added to your inventory.\n";
		}
		
		cout << "You walk to another side of the room, and see armor.\n\n";
		
		system("pause");
		system("cls");
		
		cout << "1. ";
		a.getArmor();
		cout << "\n";
		
		cout << "2. ";
		a.getArmor();
		cout << "\n";
		
		cout << "3. ";
		a.getArmor();
		cout << "\n\n";
		
		int choose2;
		cin >> choose2;
		
		if(choose2 == 1){
			inv[numItems++] = a.getArmor();
			cout << "\n";
			a.getArmor();
			cout << " was added to your inventory.\n";
		}
		
		else if(choose2 == 2){
			inv[numItems++] = a.getArmor();
			cout << "\n";
			a.getArmor();
			cout << " was added to your inventory.\n";
		}
		
		else if(choose2 == 3){
			inv[numItems++] = a.getArmor();
			cout << "\n";
			a.getArmor();
			cout << " was added to your inventory.\n";
		}
		
		\\..//
		
		string choosePath1;
		
		do{
		cout << "Which way do you want to go? (right/left)\n";
		
		cin >> choosePath1;
		
		if(choosePath1 == "right"){			
			\\..//
			
			if(inv[0] == w.weapon_[0]){
				\\..//
			}
			
			if(inv[0] == w.weapon_[1]){
				\\..//
			}
			
			if(inv[0] == w.weapon_[2]){
				\\..//
			}
			
			if(inv[0] == w.weapon_[3]){
				\\..//
			}
			
			if(inv[0] == w.weapon_[4]){
				\\..//
			}
			
			if(inv[0] == w.weapon_[5]){
				\\..//
			}
			
			if(inv[0] == w.weapon_[6]){
				\\..//
			}
			
			\\..//
			
			string lookChest1;
			do{
			cout << "Do you want to look in the chest? (yes/no)\n";
			cin >> lookChest1;
			cout << "\n\n";
			
			if(lookChest1 == "yes"){
				cout << "You open the chest and find some loot.\n\n";
				money+5;
				cout << "You now have " << money << "coins.\n\n";
				
				inv[0] = w.getWeapon();
				cout << "You trade your current weapon for a(n) " << inv[0] << ".\n\n";
				
				inv[1] = a.getArmor();
				cout << "You trade your current armor for " << inv[1] << ".\n\n";
				break;
			}
			
			else if(lookChest1 == "no"){
				cout << "You decide not to look in the chest, and continue to walk on the path.";
				break;
			}
			else{
				cout << "Illegal answer!\n\n";
			}
		} while(lookChest1 != "yes" || "no");
		
		\\..//
		
		}
		
		else if(choosePath1 == "left"){
			\\..//
			cout << "You are now dead, laying there, baking in the sun.\n\n";
			cout << "The End.\n\n\n";
			
			Sleep(5000);
			string again;
			do{
			cout << "Do you want to play again? (yes/no)\n\n";
			cin >> again;
			cout << "\n\n";
			
			if(again == "yes"){
				cout << "Ok.";
				inv[numItems--];
				money = 0;
				baseHealth = 20;
			}
			
			else if (again == "no"){
				break;
			}
		} while(again != "yes" || "no");
			
		}
		
		else{
			cout << "\n\nNot one of the choices!\n\n";
		} 
		} while(choosePath1 != "right" || "left");
		\\..//
		
	} while(game == false);
	
	return 0;
}
This is the enemy class

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 <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

class enemy{
	public:
		void genRandEnemy();
		void genRandDragon();
		void genRandBoss();
};

void genRandEnemy() {

	string enemy[] = {"troll", "wolf", "wraith", "spider", "scorpion", "hydra", "snake", "reaper", "centipede", "worm"};
	string enemyType[] = {"hell", "ice", "soul eater", "bone", "carnivorous"};
	
	srand(time(0));	
	int randomEnemy = rand();
	int randomEnemyType = rand();
	
	int randEnemy = (randomEnemy % 10);
	int randEnemyType = (randomEnemyType % 5);
	
	cout << enemyType[randEnemyType];	
	cout << " ";
	cout << enemy[randEnemy];
	
}

void genRandDragon() {
	string dragon = "dragon";
	string dragonType[] = {"hell", "soul crusher", "ice", "acient", "elder", "bone", "reaper"};
	
	srand(time(0));
	int randomDragonType = rand();
	int randDragonType = (randomDragonType % 7);
	
	cout << dragonType[randDragonType];
	cout << " ";
	cout << dragon;
}

void genRandBoss() {
	string boss[] = {"neom", "phatom"};
	string bossType[] = {"bone", "soul eater", "water", "hell", "ice"};
	
	srand(time(0));
	int randomBoss = rand();
	int randomBossType = rand();
	
	int randBoss = (randomBoss % 2);
	int randBossType = (randomBossType % 6);
	
	cout << bossType[randBossType];
	cout << " ";
	cout << boss[randBoss];
}


Weapon class:

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

class weapon {
	public:
		int getWeapon();
		string weapon_[];
};

	string weapon_[] = {"dagger", "sword", "war hammer", "mace", "great sword", "battle axe", "war axe"};
	string bow[] = {"bow", "long bow", "recurve bow", "composite bow"};

	//srand(time(0));
	int randomWeapon = rand();
	int randWeapon = (randomWeapon % 7);

	int randomBow = rand();
	int randBow = (randomBow % 4);

int getWeapon() {
	
	//m.getMaterial();	
	//cout << " ";
	weapon_[randWeapon];
}


Armor class:

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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

class armor{
	public:
		int getArmor();
};

	string armor_[] = {"helmet", "chestplate", "greives", "boots", "gauntlets"};

	//srand(time(0));	
	int randomArmor = rand();
	int randArmor = (randomArmor % 5);

int getArmor() {
	
	//m.getMaterial();
	//cout << " ";
	cout << armor_[randArmor];
}


This isn't in use, but it's the material class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

class material{
	public:
		void getMaterial();
};

void material::getMaterial() {
		string material_[7] = {"wood", "stone", "iron", "steel", "gold", "mithril", "ebony"};
	
		srand(time(0));
		int randomMaterial = rand();
		int randMaterial = (randomMaterial % 7);
}
http://www.cplusplus.com/forum/general/140198/

¿why did you think it was a good idea to post an image, when the error messages are text?
i couldn't copy it, otherwise i would have posted it as text
Right-click and "copy all", or ctrl+shift+c should perhaps work?
Ok.. i think i got it now --

It said this in the log
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
E:\The Gem Text Version\enemy.o	enemy.cpp:(.text+0x0): multiple definition of `genRandEnemy()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x0): first defined here
E:\The Gem Text Version\enemy.o	enemy.cpp:(.text+0x54f): multiple definition of `genRandDragon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x54f): first defined here
E:\The Gem Text Version\enemy.o	enemy.cpp:(.text+0x873): multiple definition of `genRandBoss()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x873): first defined here
E:\The Gem Text Version\weapon.o	weapon.cpp:(.text+0x0): multiple definition of `getWeapon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0xbac): first defined here
E:\The Gem Text Version\weapon.o	weapon.cpp:(.bss+0x0): multiple definition of `weapon_'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x0): first defined here
E:\The Gem Text Version\weapon.o	weapon.cpp:(.bss+0x1c): multiple definition of `bow'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x1c): first defined here
E:\The Gem Text Version\weapon.o	weapon.cpp:(.bss+0x2c): multiple definition of `randomWeapon'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x2c): first defined here
E:\The Gem Text Version\weapon.o	weapon.cpp:(.bss+0x30): multiple definition of `randWeapon'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x30): first defined here
E:\The Gem Text Version\weapon.o	weapon.cpp:(.bss+0x34): multiple definition of `randomBow'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x34): first defined here
E:\The Gem Text Version\weapon.o	weapon.cpp:(.bss+0x38): multiple definition of `randBow'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x38): first defined here
E:\The Gem Text Version\armor.o	armor.cpp:(.text+0x0): multiple definition of `getArmor()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0xbb1): first defined here
E:\The Gem Text Version\armor.o	armor.cpp:(.bss+0x0): multiple definition of `armor_'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x3c): first defined here
E:\The Gem Text Version\armor.o	armor.cpp:(.bss+0x14): multiple definition of `randomArmor'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x50): first defined here
E:\The Gem Text Version\armor.o	armor.cpp:(.bss+0x18): multiple definition of `randArmor'
E:\The Gem Text Version\main.o	main.cpp:(.bss+0x54): first defined here
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1047): undefined reference to `weapon::getWeapon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1079): undefined reference to `weapon::getWeapon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x10ab): undefined reference to `weapon::getWeapon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x10e4): undefined reference to `weapon::getWeapon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1127): undefined reference to `weapon::getWeapon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1152): more undefined references to `weapon::getWeapon()' follow
E:\The Gem Text Version\main.o	main.cpp:(.text+0x125e): undefined reference to `armor::getArmor()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1290): undefined reference to `armor::getArmor()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x12c2): undefined reference to `armor::getArmor()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x12fb): undefined reference to `armor::getArmor()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x133e): undefined reference to `armor::getArmor()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1369): more undefined references to `armor::getArmor()' follow
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1559): undefined reference to `enemy::genRandEnemy()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x1962): undefined reference to `weapon::getWeapon()'
E:\The Gem Text Version\main.o	main.cpp:(.text+0x19b2): undefined reference to `armor::getArmor()'
c:\program files (x86)\dev-cpp\mingw32\mingw32\bin\ld.exe	main.o: bad reloc address 0x1b in section `.text$_ZNSt11char_traitsIcE7compareEPKcS2_j[__ZNSt11char_traitsIcE7compareEPKcS2_j]'
E:\The Gem Text Version\collect2.exe	[Error] ld returned 1 exit status
26		E:\The Gem Text Version\Makefile.win	recipe for target '"The' failed
again http://www.cplusplus.com/forum/general/140198/
you are including source files, don't do that
1
2
3
4
5
6
7
8
9
10
class enemy{
	public:
		void genRandEnemy();
		void genRandDragon();
		void genRandBoss();
};

void genRandEnemy() {
//...
}
You are declaring a member function `enemy::genRandEnemy()', but you are defining a non-member function `genRandEnemy()'.
If you don't use the state of the object (anyone would work the same) consider just using a non-member function.

To define a member function
1
2
3
4
5
6
7
8
9
10
11
class foo{
public:
   void inside(){
      //...
   }
   void outside();
};

void foo::outside(){
  //...
}



It seems that you don't have any header file. http://www.cplusplus.com/faq/compiling/files/#headers
http://www.cplusplus.com/faq/beginners/multiple-sources/

(as a quick and dirty fix you may define all the functions inline)


You've got a lot of globals. Some you may want to reconsider them.
For the ones that you do want to be global refer again to http://www.cplusplus.com/forum/general/140198/


By the way, I would like to see the command used to build.
Also, you were not experiencing a crash but a build (linking) error
Last edited on
(as a quick and dirty fix you may define all the functions inline)

@ ne555: Now there's an interesting habit to encourage. I know that GCC will support it but won't the linker complain before it gets that far?
sorry, I don't understand why the linker could fail.
inline function definitions must be available at compile time, and the easiest way to do that is to put them in a header.
as such the linker would threat them especially (`nm' shows them marked as "W: weak object", not understand the meaning tough)


As for consequences of defining inline functions in a header, you'll get extra compilation time (not too small an issue), and perhaps some circularity issues with the inclusion of the headers.
Last edited on
I guess I don't know enough about how the linker treats inline functions, I've never really needed them and just trusted the optimizer to do it's job. I know that the errors that OP posted are from the linker and not the compiler, it says so right on Line 44 of his last post. I know from taking things apart that GCC and cl both still put out-of-line code in the binary for in-lined functions whether or not they are ever used. I guess I have some reading to do here.
Topic archived. No new replies allowed.