Polymorphism/inheritance

I made this game that's supposed to help guitarists learn all the chord and scale forms, and I've been trying to make the more advanced levels of my game more challenging. I decided that I needed two of the member functions in my struct to function differently when the user gets past level 4, and it occurred to me that this is a perfect opportunity to try out polymorphism. I'm still pretty new to C++, and I learned about polymorphism a few months ago, but I couldn't think of a use for it until now.

I have a struct called Scaleform and I'm trying to create a derived class called AdvScaleForm that's the same except that the member functions RootWack and ShowRootCand are different. So I followed the example in my C++ book: I made those two functions virtual, and I made new versions of the functions and put them into struct AdvScaleForm:public ScaleForm. And I changed my class object to a pointer. But I got this compile error: "no matching function for call to 'AdvScaleForm::AdvScaleForm(int&, int&, int&)'" for this line:
pSf.push_back(new AdvScaleForm(pos, form, mode));

It seems like it's not finding the class constructor, but I don't know why because I thought the constructor was automatically inherited. Here's a truncated version of my structs and main(). I'm assuming the problem is in the AdvScaleForm part, which is shown in its entirety. Thanks for your help.

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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
struct ScaleForm
{
	short int m_Fingering[5][3];
	short int m_Length[5];
	short int m_UsrFingering[5][3];
	short int m_UsrLength[5];
	short int m_RootCand[5][5];
	short int m_Form, m_Span, m_Position, m_RootNum, m_Mode, m_CandSt, m_CandFr, m_LastCand1, m_LastCand2, m_Goal, m_WhackCurrent, m_Correct;
	int m_WhackGuess;
	unsigned long int m_Goodtime;
	bool m_Wack, m_First;
	
	ScaleForm(short int, short int, short int);
	void ShowAnswer();
	void ShowUsr(bool);
	bool HearAnswer();
	bool HearUsr();
	void ShowString(short int str, short int fing[][3]);
	short int HearString(short int str, short int fing[][3]);
	void ShowGhost(short int, short int);
	void ShowMarkers();
	bool UpdateUsr(short int, short int);
	short int GetUsrLength(short int);
	bool IsCorrect();
	bool IsStringFull(short int);
	bool IsSpanFull(short int);
	bool AreAllFull();
	short int GetNext(short int);
	short int GetRndNext();
	void ShowNext(short int, short int);
	bool virtual RootWack(bool);
	short int virtual ShowRootCand();
	void ShowRootStar();
	void ShowGood();
	void WackInfo();
};

ScaleForm::ScaleForm(short int posit, short int frm, short int mode)
{
	m_Form = frm;
	m_Span = ALL_SPANS[frm];
	m_Position = posit;
	m_Mode = mode;
	m_RootNum = ROOT_NUMBERS[frm] + posit;
	m_Goal = WHACK_GOALS[level];
	m_WhackCurrent = WHACK_START_LEVELS[level];
	m_WhackGuess = -1;
	m_Correct = 1;
	m_First = false;
	
	// etc, etc...
}

// a bunch of member functions removed...

bool ScaleForm::RootWack(bool goodness)
{
	// the original version of my virtual member function goes here.
}

short int ScaleForm::ShowRootCand()
{	
	// the original version of my other virtual member function goes here.
}

// more member functions removed...

/*
* below is the complete text of my derived class. nothing was removed.
* but you probably won't need to look at all the member function code,
* as I've already verified that the functions themselves work fine
* if I put them into the parent class (with the "ScaleForm::" prefix
* removed from all the member function calls).
*/

struct AdvScaleForm:public ScaleForm
{
	bool virtual RootWack(bool);
	short int virtual ShowRootCand();
};

bool AdvScaleForm::RootWack(bool goodness)
{
	short int z = -1;
	unsigned long int time_initial;
	m_Wack = false;
	m_Goodtime = 0;
	m_WhackGuess = -1;
	m_First = true;
	
	while ((!m_Wack) && (z != 1) && (z != 3) && (z != 5))
	{
		if (goodness)
		{
			goodness = false;
			m_Goodtime = SDL_GetTicks();
			apply_surface( 0, 0, background, screen );
			apply_surface(-20, 150, neck, screen);
			ScaleForm::ShowAnswer();
			ScaleForm::ShowMarkers();
			ScaleForm::ShowGood();
			ScaleForm::ShowRootStar();
			ScaleForm::WackInfo();
			SDL_Flip(screen);
			SDL_Delay(500);
		}
		
		apply_surface(0, 0, background, screen);
		apply_surface(-20, 150, neck, screen);
		ScaleForm::ShowAnswer();
		ScaleForm::ShowMarkers();
		ScaleForm::ShowGood();
		ScaleForm::WackInfo();
		SDL_Flip(screen);
		SDL_Delay(100);
		
		if (m_First)
		{
			Mix_PlayMusic(rootWhackMusic[m_WhackCurrent], -1);
		}
		
		apply_surface( 0, 0, background, screen );
		apply_surface(-20, 150, neck, screen);
		ScaleForm::ShowAnswer();
		ScaleForm::ShowMarkers();
		ScaleForm::ShowGood();
		ScaleForm::WackInfo();
		
		do 
		{
			m_CandSt = rand() % 5;
			m_CandFr = rand() % 5;
			z = ShowRootCand();
		} while (z == -1);
		
		SDL_Flip(screen);
		
		time_initial = SDL_GetTicks();
		m_Wack = false;
		
		while ((SDL_GetTicks() - time_initial) < WACK_RATES[m_WhackCurrent])
		{
			if ((SDL_PollEvent(&event)) && (event.type == SDL_KEYDOWN))
			{
				m_WhackGuess = event.key.keysym.sym;
				
				if ((m_WhackGuess == 49) || (m_WhackGuess == 51) || (m_WhackGuess == 53) || (m_WhackGuess == 257) || (m_WhackGuess == 259) || (m_WhackGuess == 261))
				{
					m_Wack = true;
					if (m_WhackGuess < 54)
					{
						m_WhackGuess -= 48;
					}
					else 
					{
						m_WhackGuess -= 256;
					}
					break;
				}
				else 
				{
					m_WhackGuess = -1;
				}
			}
		}
	}
		
	if (m_WhackGuess == z)
	{
		whackNum++;
		m_WhackCurrent++;
		m_Correct = m_WhackGuess;
		Mix_HaltMusic();
		if (whackNum < m_Goal)
		{
			Mix_PlayChannel(-1, winNote[NOTE_MAP[m_CandSt][m_CandFr + m_Position]], 0);
		}
		else 
		{
			Mix_PlayChannel(-1, noteSnd[NOTE_MAP[m_CandSt][m_CandFr + m_Position]], 0);
		}
	}
	
	return (m_WhackGuess == z);
}

short int AdvScaleForm::ShowRootCand()
{
	if ((m_CandSt == m_LastCand1) && (m_CandFr == m_LastCand2))
	{
		return -1;
	}
	
	if (m_RootCand[m_CandSt][m_CandFr] != -1)
	{
		if ((m_First) && (m_RootCand[m_CandSt][m_CandFr] == 1))
		{
			return -1;
		}
		
		m_First = false;
		
		m_LastCand1 = m_CandSt;
		m_LastCand2 = m_CandFr;
		
		if ((m_CandFr == 1) || (m_CandFr == 3))
		{
			apply_surface(FRETS[m_CandFr + m_Position - 1], STRINGS[m_CandSt][m_CandFr + m_Position - 1] - 20, rightRootStar[0][DOT_WIDTH[m_CandFr + m_Position - 1]], screen);
			if (m_CandSt == 0)
			{
				apply_surface(FRETS[m_CandFr + m_Position - 1], STRINGS[5][m_CandFr + m_Position - 1] - 20, rightRootStar[0][DOT_WIDTH[m_CandFr + m_Position - 1]], screen);
			}
		}
		else 
		{
			apply_surface(FRETS[m_CandFr + m_Position] - 20, STRINGS[m_CandSt][m_CandFr + m_Position] - 20, rootStar[0], screen);
			if (m_CandSt == 0)
			{
				apply_surface(FRETS[m_CandFr + m_Position] - 20, STRINGS[5][m_CandFr + m_Position] - 20, rootStar[0], screen);
			}
		}
		
		if ((m_RootCand[m_CandSt][m_CandFr] != 1) && (m_RootCand[m_CandSt][m_CandFr] != 3) && (m_RootCand[m_CandSt][m_CandFr] != 5))
		{
			return 0;
		}
	}
	
	return m_RootCand[m_CandSt][m_CandFr];
}

vector<ScaleForm*> pSf;
int level;

int main(int argc, char* args[])
{
	int form, lastForm, pos, mode;
	
	srand(static_cast<unsigned int> (time(0)));
	
	for (level = 0; level < 9; level++)
	{
		while (lastForm == form)
		{
			form = (rand() % FORM_LEVELS[0][level]) + FORM_LEVELS[1][level];
			pos = rand() % 7;
			mode = rand() % 2;
		}
		
		pSf.clear();
		if (level < 4)
		{
			pSf.push_back(new ScaleForm(pos, form, mode));
		}
		else 
		{
			pSf.push_back(new AdvScaleForm(pos, form, mode));
		}
		
	// stuff taken out....
	}
}
Last edited on
Constructors in C++ are not inherited. In C++11 you can explicitly inherit constructors.
Last edited on
Okay, I just tried copying the constructor from ScaleFrom and pasting it into AdvScaleForm but now I'm getting this error, "no matching function for call to 'ScaleForm::ScaleForm()'" for this line (which is the constructor definition in my derived class):

AdvScaleForm::AdvScaleForm(short int posit, short int frm, short int mode)

Here's the full code for my derived class now:

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
struct AdvScaleForm:public ScaleForm
{
	AdvScaleForm(short int, short int, short int);
	bool virtual RootWack(bool);
	short int virtual ShowRootCand();
};

AdvScaleForm::AdvScaleForm(short int posit, short int frm, short int mode)
{
	m_Form = frm;
	m_Span = ALL_SPANS[frm];
	m_Position = posit;
	m_Mode = mode;
	m_RootNum = ROOT_NUMBERS[frm] + posit;
	m_Goal = WHACK_GOALS[level];
	m_WhackCurrent = WHACK_START_LEVELS[level];
	m_WhackGuess = -1;
	m_Correct = 1;
	m_First = false;
	
	if (mode == 1)
	{
		m_RootNum -= 3;
	}
	
	m_LastCand1 = -1;
	m_LastCand2 = -1;
	
	short int x = 0;
	short int it, it2;
	
	for (it = 0; it < 5; it++)
	{
		for (it2 = 0; it2 < 3; it2++)
		{
			m_Fingering[it][it2] = SYMBOLS[frm][x];
			m_UsrFingering[it][it2] = -1;
			x++;
		}
	}
	
	x = 0;
	for (it = 0; it < 5; it++)
	{
		for (it2 = 0; it2 < 5; it2++)
		{
			m_RootCand[it][it2] = ROOT_FINDER[mode][frm][x];
			x++;
		}
	}
	
	for (it = 0; it < 5; it++)
	{
		m_Length[it] = ALL_LENGTHS[frm][it];
		m_UsrLength[it] = 0;
	}
}

bool AdvScaleForm::RootWack(bool goodness)
{
	short int z = -1;
	unsigned long int time_initial;
	m_Wack = false;
	m_Goodtime = 0;
	m_WhackGuess = -1;
	m_First = true;
	
	while ((!m_Wack) && (z != 1) && (z != 3) && (z != 5))
	{
		if (goodness)
		{
			goodness = false;
			m_Goodtime = SDL_GetTicks();
			apply_surface( 0, 0, background, screen );
			apply_surface(-20, 150, neck, screen);
			ScaleForm::ShowAnswer();
			ScaleForm::ShowMarkers();
			ScaleForm::ShowGood();
			ScaleForm::ShowRootStar();
			ScaleForm::WackInfo();
			SDL_Flip(screen);
			SDL_Delay(500);
		}
		
		apply_surface(0, 0, background, screen);
		apply_surface(-20, 150, neck, screen);
		ScaleForm::ShowAnswer();
		ScaleForm::ShowMarkers();
		ScaleForm::ShowGood();
		ScaleForm::WackInfo();
		SDL_Flip(screen);
		SDL_Delay(100);
		
		if (m_First)
		{
			Mix_PlayMusic(rootWhackMusic[m_WhackCurrent], -1);
		}
		
		apply_surface( 0, 0, background, screen );
		apply_surface(-20, 150, neck, screen);
		ScaleForm::ShowAnswer();
		ScaleForm::ShowMarkers();
		ScaleForm::ShowGood();
		ScaleForm::WackInfo();
		
		do 
		{
			m_CandSt = rand() % 5;
			m_CandFr = rand() % 5;
			z = ShowRootCand();
		} while (z == -1);
		
		SDL_Flip(screen);
		
		time_initial = SDL_GetTicks();
		m_Wack = false;
		
		while ((SDL_GetTicks() - time_initial) < WACK_RATES[m_WhackCurrent])
		{
			if ((SDL_PollEvent(&event)) && (event.type == SDL_KEYDOWN))
			{
				m_WhackGuess = event.key.keysym.sym;
				
				if ((m_WhackGuess == 49) || (m_WhackGuess == 51) || (m_WhackGuess == 53) || (m_WhackGuess == 257) || (m_WhackGuess == 259) || (m_WhackGuess == 261))
				{
					m_Wack = true;
					if (m_WhackGuess < 54)
					{
						m_WhackGuess -= 48;
					}
					else 
					{
						m_WhackGuess -= 256;
					}
					break;
				}
				else 
				{
					m_WhackGuess = -1;
				}
			}
		}
	}
		
	if (m_WhackGuess == z)
	{
		whackNum++;
		m_WhackCurrent++;
		m_Correct = m_WhackGuess;
		Mix_HaltMusic();
		if (whackNum < m_Goal)
		{
			Mix_PlayChannel(-1, winNote[NOTE_MAP[m_CandSt][m_CandFr + m_Position]], 0);
		}
		else 
		{
			Mix_PlayChannel(-1, noteSnd[NOTE_MAP[m_CandSt][m_CandFr + m_Position]], 0);
		}
	}
	
	return (m_WhackGuess == z);
}

short int AdvScaleForm::ShowRootCand()
{
	if ((m_CandSt == m_LastCand1) && (m_CandFr == m_LastCand2))
	{
		return -1;
	}
	
	if (m_RootCand[m_CandSt][m_CandFr] != -1)
	{
		if ((m_First) && (m_RootCand[m_CandSt][m_CandFr] == 1))
		{
			return -1;
		}
		
		m_First = false;
		
		m_LastCand1 = m_CandSt;
		m_LastCand2 = m_CandFr;
		
		if ((m_CandFr == 1) || (m_CandFr == 3))
		{
			apply_surface(FRETS[m_CandFr + m_Position - 1], STRINGS[m_CandSt][m_CandFr + m_Position - 1] - 20, rightRootStar[0][DOT_WIDTH[m_CandFr + m_Position - 1]], screen);
			if (m_CandSt == 0)
			{
				apply_surface(FRETS[m_CandFr + m_Position - 1], STRINGS[5][m_CandFr + m_Position - 1] - 20, rightRootStar[0][DOT_WIDTH[m_CandFr + m_Position - 1]], screen);
			}
		}
		else 
		{
			apply_surface(FRETS[m_CandFr + m_Position] - 20, STRINGS[m_CandSt][m_CandFr + m_Position] - 20, rootStar[0], screen);
			if (m_CandSt == 0)
			{
				apply_surface(FRETS[m_CandFr + m_Position] - 20, STRINGS[5][m_CandFr + m_Position] - 20, rootStar[0], screen);
			}
		}
		
		if ((m_RootCand[m_CandSt][m_CandFr] != 1) && (m_RootCand[m_CandSt][m_CandFr] != 3) && (m_RootCand[m_CandSt][m_CandFr] != 5))
		{
			return 0;
		}
	}
	
	return m_RootCand[m_CandSt][m_CandFr];
}
When you provide your own constructor, the ones the compiler would have provided for you are not provided. Prior to C++11 you need to manually add your default constructor, but in C++11 and above you an explicitly ask the compiler to generate it for you.

You may want to reread the tutorial on this site as it covers the problems you're running into.
Last edited on
Woo hoo! I checked my book again and I managed to figure it out. I just needed to change my constructor to this:

1
2
3
AdvScaleForm::AdvScaleForm(short int posit, short int frm, short int mode):
ScaleForm(posit, frm, mode)
{}


Thanks for your help.
Topic archived. No new replies allowed.