This simple code compiles and runs fine using VC++ 10:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Foo
{
public:
Foo();
Foo(int i, string s) { data = make_pair(i, s); };
bool const operator==(const Foo& f) { return f.data == data; };
private:
pair<int, string> data;
int stuff;
};
int _tmain(int argc, _TCHAR* argv[])
{
Foo f1(10, "xyz");
Foo f2(10, "xyz");
cout << ((f1 == f2) ? "equal" : "not equal") << endl;
return 0;
}
However, if I change the Foo class so that the pair now consists of two of my classes:
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Bar
{
public:
Bar();
Bar(string, string);
bool const operator==(const Bar& b) { return b.s1 == s1 && b.s2 == s2; };
private:
string s1;
string s2;
int k;
};
class Foo
{
public:
Foo();
Foo(Bar b1, Bar b2) { data = make_pair(b1, b2); };
bool const operator==(const Foo& f) { return f.data == data ; };
private:
pair<Bar, Bar> data;
int stuff;
};
int _tmain(int argc, _TCHAR* argv[])
{
Bar b1("abc", "xyz");
Bar b2("abc", "xyz");
Foo f1(b1, b2);
Foo f2(b1, b2);
cout << ((f1 == f2) ? "equal" : "not equal") << endl;
return 0;
}
it will not compile and I get:
1>------ Build started: Project: pair_test, Configuration: Debug Win32 ------
1> pair_test.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\utility(305): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Bar' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> c:\program files\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> c:\program files\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> c:\program files\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> c:\program files\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> c:\documents and settings\charles\my documents\visual studio 2010\projects\pair_test\pair_test.cpp(12): or 'const bool Bar::operator ==(const Bar &)'
1> while trying to match the argument list '(const Bar, const Bar)'
1> c:\documents and settings\charles\my documents\visual studio 2010\projects\pair_test\pair_test.cpp(24) : see reference to function template instantiation 'bool std::operator ==<Bar,Bar>(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' being compiled
1> with
1> [
1> _Ty1=Bar,
1> _Ty2=Bar
1> ]
1>c:\program files\microsoft visual studio 10.0\vc\include\utility(305): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Bar' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1> c:\program files\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1> c:\program files\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1> c:\program files\microsoft visual studio 10.0\vc\include\system_error(408): or 'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1> c:\program files\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1> c:\documents and settings\charles\my documents\visual studio 2010\projects\pair_test\pair_test.cpp(12): or 'const bool Bar::operator ==(const Bar &)'
1> while trying to match the argument list '(const Bar, const Bar)'
It looks like I'm missing something obvious here, but searching hasn't helped much. Do I have to explicitly define the == operator for pair<bar,>? The generic == operator should work fine if it knows how to compare Bar objects, I would think.