Hi,
I am encountering a compiler error that I am not able to resolve. I wrote a simple test program to demonstrate it:
Code:
#include <iostream>
#include <list>
using std::cout;
using std::endl;
using std::list;
class TestClass {
public:
TestClass(int a) { somedummydata = a; }
bool operator==(const TestClass &other) { return (somedummydata == other.somedummydata); }
bool operator!=(const TestClass &other) { return (somedummydata != other.somedummydata); }
private:
int somedummydata;
};
int main() {
list<TestClass> *list1 = new list<TestClass>;
list<TestClass> *list2 = new list<TestClass>;
for (unsigned int i = 0; i < 10; ++i) {
TestClass a(i);
list1->push_back(a);
list2->push_back(a);
}
if (*list1 == *list2) {
cout << "a" << endl;
}
}
the code does not compile. Here is the compiler output:
Code:
/usr/include/c++/4.1.3/bits/stl_list.h: In function ‘bool std::operator==(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&) [with _Tp = TestClass, _Alloc = std::allocator<TestClass>]’:
test.cpp:30: instantiated from here
/usr/include/c++/4.1.3/bits/stl_list.h:1174: error: passing ‘const TestClass’ as ‘this’ argument of ‘bool TestClass::operator==(const TestClass&)’ discards qualifiers
I don't quite understand the error. Can someone please tell me what I am doing wrong?
The output is from GCC 4.1 on Linux.
Thank you very much