base class undefined?

My single error is that Base class SlotMachine is undefined.
I have no idea how i am getting this error. To my knowledge im defining everything just fine. is the error somewhere else?
This error is not letting me compile and move forward on this.Help is greatly appreciated!

here is the entirety of 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
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
225
226
  //=============================================
// slotmachine.h
// Decleration of the slot machine class
//=============================================

#ifndef __SLOTMACHINE_H__
#define __SLOTMACHINE_H__

#include<iostream>

/*
	Slot Machine class
	Models the behavior of a slot machine
*/
class SlotMachine
{
public:
	static int m_creditpool;
	static int m_rewardpool;
	SlotMachine();
	int play();
	void add_quarters( int quarters );
	int check_quarters() const;
};

#endif // __SLOTMACHINE_H__

#include<stdlib.h>
#include"slotmachine.h"

int SlotMachine::play()
{ 
	 //won = false;
	if( m_creditpool > 0 )
	{
		m_rewardpool += 1;
		m_creditpool -= 1;

		if(m_rewardpool >= 2)
		{
			bool won = rand()%2 == 0;
			if( won )
			{
				m_rewardpool -= 2;
				return 2;
			}
		}
	}
	
	return 0;
}

void  SlotMachine::add_quarters( int quarters )
{
	m_creditpool += quarters; 
}

int SlotMachine::check_quarters() const
{
	return m_creditpool;
}

SlotMachine::SlotMachine()
{
	m_creditpool = 0;
	m_rewardpool = 0;
}

#ifndef __POWERSLOTS_H__
#define __POWERSLOTS_H__

#include <iostream>

class PowerSlots : public SlotMachine
{
public:
	int play();
	int PowerPlay();
};

#endif

#include"PowerSlots.h"
#include<stdlib.h>
#include"slotmachine.h"

int PowerSlots::play()
{
	if( SlotMachine::m_creditpool > 0 )
	{	
		SlotMachine::m_creditpool--;
		SlotMachine::m_rewardpool++;

		if( SlotMachine::m_rewardpool > 2 )
		{
			bool won = rand() % 5 == 0;
			if( won )
			{
				SlotMachine::m_rewardpool -= 2;
				return 2;
			}
		}
	}

	return 0;
}

int PowerSlots::PowerPlay()
{
	if( SlotMachine::m_creditpool > 2 )
	{
		SlotMachine::m_creditpool -= 2;
		SlotMachine::m_rewardpool += 2;
		
		if( SlotMachine::m_rewardpool > 10 )
		{
			bool won = rand() % 5 == 0;
			if(won)
			{
				SlotMachine::m_rewardpool = SlotMachine::m_rewardpool / 2;
				return SlotMachine::m_rewardpool;
			}
		}
	}

	return 0;
}

#include "slotmachine.h"
#include"PowerSlots.h"
#include<time.h>
#include<iostream>


int main()
{
	//srand(time(NULL)); //seed
	
	SlotMachine one;
	PowerSlots two;

	bool play = true;
	std::cout << "Lets play the Slot Machine! \n" << std::endl;
	while(play)
	{
		//add credits
		int quarters;
		std::cout << "All quarters you enter will be consumed in 1 round.";
		std::cout << "Enter how many quarters you want to play: ";
		std::cin >> quarters;
		std::cout << "\n" << std::endl;
		
		// choose which mode to play, normal or power
		std::cout << "Normal \n" 
			<< "or \n"
			<< "PowerPlay!\n" << std::endl
			<< "Press 1 for Normal\n"
			<< "Press 2 for Powerplay: ";
		int n_p;
		std::cin >> n_p;
		
		//play for normal mode
		if(n_p == 1)
		{
			one.add_quarters(quarters);
			while( one.check_quarters() > 0 )
			{
				for(int i = 0; i < quarters; ++i)
				{
					one.add_quarters(quarters);
				}

				quarters = 0;
				quarters += one.play();

				if(quarters == 0)
				{
					std::cout << "You lost." << std::endl;
				}
				else
				{
					std::cout << "you win. " << std::endl;
				}
			}
		}
		//play for powerplay mode
		else if( n_p == 2 )
		{
			two.add_quarters(quarters);
			while( two.check_quarters() >= 10 )
			{
				for( int i = 0; i < quarters; ++i )
				{
					one.add_quarters(quarters);
				}

				quarters = 0;
				quarters += two.PowerPlay();

				if( quarters == 0 )
				{
					std::cout << "You lost." << std::endl;
				}
				else
					std::cout << "You win." << std::endl;
			}
		}

		std::cout << "You have: " << one.check_quarters() << "quarters.\n"
			<< "Do you wish to continue playing?\n" << std::endl 
			<< "Press 1 for yes.\n" 
			<< "Press 2 for no.\n" ;
		int cont;
		std::cin >> cont;
		std::cout << "\n\n";

		if(cont == 1)
			play;
		else if (cont == 2)
			play = false;
	}
	std::cout << "Thanks for playing!\n\n";
	system("pause");

	return 0;
}
Does your powerslots.h #inlcude slotmachine.h ? Or just powerslots.cpp does? It should.
Also, you try to initialize static fields
1
2
static int m_creditpool;
static int m_rewardpool;

inside a constructor. They are static, so you should define them outside class, best in class .cpp file like this:
1
2
int SlotMachine::m_creditpool = 0;
int SlotMachine::m_rewardpool = 0;
Last edited on
@JockX
What do you mean define them outside of class?
do you mean i should put them in slotmachine.cpp instead of slotmachine.h?
where? inside slotmachine::slotmachine()? cuz that throws me an error
as soon as i added #include slotmachine.h to powerslots, i got this error about the static ints :

Error 1 error LNK2001: unresolved external symbol "public: static int SlotMachine::m_rewardpool" (?m_rewardpool@SlotMachine@@2HA) F:\QT 2\OOP\SlotMachine\SlotMachine\PowerSlots.obj SlotMachine

and im not sure what you mean by putting those static ints outside of the class, but inside the cpp of the class.
You should define static members outside of ANY curly braces, not inside any function or class definition, just like global variables. Usually you define static members in cpp file of the related class, that is slotmachine.cpp.
In .H file leave as it is:
1
2
3
4
5
6
7
8
9
10
class SlotMachine
{
public:
	static int m_creditpool;
	static int m_rewardpool;
	SlotMachine();
	int play();
	void add_quarters( int quarters );
	int check_quarters() const;
};


and additionaly put definitions in cpp file:
1
2
3
4
5
6
7
8
9
#include<stdlib.h>
#include"slotmachine.h"

int SlotMachine::m_creditpool = 0;
int SlotMachine::m_rewardpool = 0;

int SlotMachine::play()
{ 
	//(... etc) 


And as a sidenote: is there a real reason to make these variables static? You mean to create many machines of the same type, that would share these variables? Maybe it would be easier to make them normal instance variables, and forget about all the above?
Last edited on
m_creditpool and m_rewardpool were private at first and i was trying to access them from a derived class. apperantly making them static is the only way?
To access class fields from derived class make them protected:, making them static do not affect their privateness.

A quick review:
public: - all classes can access these members
protected: - only this class and derived classes may access these members
private: - only this class may access these members (other classes may use setters and getters, if such exist and are public)

static - each and every object of this class share this field. If you change its value in one object, all objects will be affected. You can access public static fields even if no objects of this class are created.
Last edited on
oh wow, i see. thanks for that. greatly appreciated.

i was able to advance the game much further since it compiled,

and now you just helped me figure out why all my slot machines had the same rewardpool lmao. thank you!
Topic archived. No new replies allowed.