overload error

Hello, so im getting overload function with no contextual type information at lines 124 & 131 . Any help would be appreciated

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
#ifndef Guess_h
#define Guess_h
#include <iostream>
#include <string>
#include <algorithm>  // used for sort
#include <vector>  // for vector
#include <sstream>
#include <fstream>
#include <limits>
#include <cstdlib>
using namespace std;

/////////////////////////
// Player Class
/////////////////////////

class Player 
{
public:
virtual int getGuess() = 0;
virtual void lastGuess(int result) = 0;
virtual void lastGuess1(int result) = 0;
virtual void hGuess2(double result) = 0;
virtual void hGuess3(double result) = 0;
};


/////////////////////////
// ComputerPlayer Class
/////////////////////////

class ComputerPlayer : public Player
{
public:
ComputerPlayer();
int getGuess();
virtual void lastGuess(int result);
virtual void lastGuess1(int result);
virtual void hGuess2(double result) {};
virtual void hGuess3(double result) {};
void setmin(int mi) {min = mi;};
void setmax(int ma) {max = ma;};
int returnGuess() {cout << guess << endl;return guess;}

private:
int min = 0, max = 99;
// Bounds to guess within
int guess;
// The guess we made
};

ComputerPlayer::ComputerPlayer() : Player()
{
min = 0;
max = 99;
guess = 0;

}

int ComputerPlayer::getGuess()
{
// Make guess where min < guess <= max
int guess = (rand() % (max -min)) + min;
cout << "The computer guesses " << guess << endl;
// Remember the guess
this->guess = guess;
return guess;
}

// Tells the computer player if the last guess was
// too low or too high.  This version only gets feedback
// on our own guess, not on the opponent's guess.  It would
void ComputerPlayer::lastGuess(int result)
{
// implement your code here

		cout << "last guess function high " << endl;
	//max = result;
	setmax(result);
		//cout << max << end;
guess = result;
}
void ComputerPlayer::lastGuess1(int result)
{
// implement your code here
		cout << "last guess function low " << endl;
		//min = result;
		setmin(result);
		cout << min << endl;
guess = result;
}
/////////////////////////
// HumanPlayer Class
/////////////////////////
class HumanPlayer : public Player
{
public:
HumanPlayer();
int getGuess();
virtual void lastGuess(int result) {};
virtual void lastGuess1(int result) {};
virtual void hGuess2(double result);
virtual void hGuess3(double result);

private:
int guess;
};
HumanPlayer::HumanPlayer() : Player()
{
}
int HumanPlayer::getGuess()
{
	cout << "Please enter your guess: " << endl;
	cin >> guess;
//implement your code here
// Ask user to input guess
//  get guess from the human player
return guess;
}

void HumanPlayer::hGuess2(double result)
{
	cout << "I want to change the max " << endl;
	 max = result;                  // ***************** This is where error

// Do nothing, human can see the screen
}
void HumanPlayer::hGuess3(double result)
{
		cout << "I want to change the min " << endl;
		min = result;          // ***************** This is where error

// Do nothing, human can see the screen
}

//////////////////////////////////////
// Global functions 
//////////////////////////////////////
bool checkForWin(int guess, int answer, Player &p)
{
	bool win = false;
	if (guess > answer)
	{
		cout << "Your guess was to high " << endl;
		p.lastGuess(guess);
				p.hGuess2(guess);
		win = false; 
	}
	else if(guess < answer)
	{
		cout << "Your guess was to low " << endl;
		p.lastGuess1(guess);
		p.hGuess3(guess);
		win = false; 
	}
	if ( guess == answer)
	{
		cout << "You win!!!" << endl;
		win = true; 
	}
//  if the guess is same as answer. The player wins.
//  if the guess is not the same as answer
//  inform the palyer if the guess is too high or too low
//  and keep track of last guess
// 
return win;
}
void play(Player &player1, Player &player2)
{
int answer = 0, guess = 0;
answer = rand() % 100;
cout << answer << endl;
// get an ramdom answer for play to guess
bool win = false;
while (!win)
{
cout << "Player 1's turn to guess." << endl;
guess = player1.getGuess();
win = checkForWin(guess, answer, player1);
if (win) return;
cout << "Player 2's turn to guess." << endl;
guess = player2.getGuess();
win = checkForWin(guess, answer, player2);
}
}
Last edited on
Hi,

This might be because of line 11. std::min and std::max are defined in the algorithm header.

using namespace std; is a bad habit, now might be the time to break it :+)

If you look at the code provided by many of the experienced users here, they always put std:: before each std thing. It's the best way. There are other alternatives, but doing that is the best in the end.

Some other things to help you out:

When overriding a pure virtual function, use the override keyword. C++11

Consider using the member initilization list rather than assignment in the constructor.

Use const more, for member functions that don't change the state of the object, in function parameters, and basically where ever you can.

Good Luck !!
Thank you!!! just changing the variable names from max/ min to high/lo fixed it!!

Ive always wondered why is it a bad habit to use using namespace std;. What are the down sides? and what are the benefits to using std::
What are the down sides? and what are the benefits to using std::


Well the main downside you have already encountered: name clashes. That is the whole point of namespaces - to avoid name clashes, using namespace std; subverts that in a big way by bringing in all of the STL (as defined by whatever include files there are) into the global namespace and polluting it. There are lots of examples of normal words used in the STL: left, right, distance to name just a few. Ideally one should put their own code into it's own namespaces.

Also, when one uses std:: , it specifies which thing one is using. For example, std::copy , not boost::copy , or copy from some other library.

Another thing I noticed in your code: Avoid using std::endl , it flushes the buffer every time, so could be inefficient. Just use "\n" instead:

std::cout << "Player 1's turn to guess.\n";
Last edited on
Topic archived. No new replies allowed.