Problem with creating a comparison operator: the object has type qualifiers that are not compatible with the member function

Hello,

So I am sure this is simple; I am attempting to create a comparision operator for a complex class; I recreated the problem below with a simple class.

The error message I get with the "BaseClass" below is:

1
2
3
IntelliSense: the object has type qualifiers that are not compatible with the member function
            object type is: const BaseClass


I am getting this error in my "BaseClass::operator<" implementation.

What gives? Thank you!


BaseClass.h:
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
#ifndef BASECLASS_H_
#define BASECLASS_H_

class BaseClass {

public:

	BaseClass();
	~BaseClass();

	virtual float const getInternalValue();
	virtual void setInternalValue(float valueToSet);

	virtual float const getTotalValue();

	virtual bool operator< (const BaseClass& rightHandSide);

protected:

	float internalValue;

private:

};

#endif 


BaseClass.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
28
29
30
31
32
33
34
35
36
37
#include "BaseClass.h"

BaseClass::BaseClass() : internalValue(1.0f) {



}

BaseClass::~BaseClass() {



}

float const BaseClass::getInternalValue() {

	return internalValue;

}

void BaseClass::setInternalValue(float valueToSet) {

	internalValue = valueToSet;

}

float const BaseClass::getTotalValue() {

	return internalValue;

}

bool BaseClass::operator< (const BaseClass& rightHandSide) {

	return this->getTotalValue() < rightHandSide.getTotalValue();

}
It is not wrong to write it like so: virtual float const getInternalValue();

But I guess what you really want is virtual float getInternalValue() const;
Now it's a const function and you may call it even if the BaseClass object is const.

The operator< should also be const:
1
2
3
4
5
6
7
8
9
...
	virtual bool operator< (const BaseClass& rightHandSide) const;
...

bool BaseClass::operator< (const BaseClass& rightHandSide) const {

	return this->getTotalValue() < rightHandSide.getTotalValue();

}
Topic archived. No new replies allowed.