Hi, I've been having a few headaches trying to get my code to compile. I feel like I've tried every way of changing "const"-ness, but still can't get this to compile with g++ in a cygwin environment.
Would you be so kind to tell me where on earth I am going wrong? Below is the code and the compiler error. The error talks about "qualifiers" which I assume mean there's a "const" missing somewhere, but for the life of me, I can't figure out where.
Code:
#include <iostream>
using namespace std;
class Test
{
public:
Test() : val(0) {}
Test(int v) : val(v) {}
int getVal() { return val; }
bool operator<(const Test& rhs) {
if (rhs.val < val) return false;
else return true;
}
private:
int val;
};
template <class T>
inline const T& mymax(const T& a, const T& b)
{
if (a < b) return b;
else return a;
}
int main(int argc, char* argv)
{
int a1 = 10, a2 = 20;
const Test t1(10);
const Test t2(20);
int result = mymax(a1,a2);
const Test result2 = mymax(t1,t2);
cout << "Largest value of ints is: " << result << endl;
cout << "Largest value of Test(ints) is: " << result2.getVal() << endl;
}
The compiler generates this error (g++):
Code:
maxTamplate.cpp: In function `const T& mymax(const T&, const T&) [with T = Test]`:
maxTemplate.cpp:37: instantiated from here
maxTemplate.cpp:26: error: passing `const Test' as `this' argument of `bool Test::operator<(const Test&)' discards qualifiers
I've no idea what this message is trying to tell me