Bool Declaration not compatible with Fan class

I need to make a Fan GUI program that asks for if the fan is on and what speed the fan is on. I've got the general code done but im getting an error with my Bool Fan::isOn() function. It's says declaration is incompatible and i cant see what i did wrong that function is the only one that's not working. Here's my code.

FanHeader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>
#include <string>
#include <limits>

using namespace std;

class Fan
{
public:
	Fan();
	int getSpeed();
	void setSpeed(int speed);
	bool isOn();
	void setOn(bool trueOrFalse);
private:
	int speed;
	bool isOn;
};


FanMpl.cpp
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
  #include "FanHeader.h"
Fan::Fan()
{
	this->speed = 1;
	this->isOn = false;
}

int Fan::getSpeed()
{
	return this->speed;
}

void Fan::setSpeed(int speed)
{
	this->speed = speed;
}

bool Fan::isOn()
{
	return false;
}

void Fan::setOn(bool trueOrFalse)
{
	this->isOn = trueOrFalse;
}
is it because you named the bool variable and the bool function the same name?
I suspect that is behind it.
Last edited on
fanheader.h
Lines 13 & 17: Your function name is the same as your variable name. This is confusing the compiler. Does isOn refer to the variable or the address of the function?

fanmpl.cpp
Line 4-5: I would argue that if the fan speed is 1, the fan is on.

You have a logical error at line 20. You should be returning isOn (or whatever you rename the variable to).

Is it really necessary to have both a speed variable and a isOn variable? I would suggest changing the isOn() function to:
1
2
3
bool Fan::isOn()
{  return speed > 0;   // if speed is 0, fan is stopped
}

By doing this, you can remove the isOn variable.
Last edited on
ah ok thanks it works now
Topic archived. No new replies allowed.