比较对象对 [英] Comparing pairs of objects

查看:100
本文介绍了比较对象对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的代码使用VC ++ 10进行编译和运行:

  #include     stdafx.h 
#include < string >
#include < iostream >

使用 命名空间标准;

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>数据;
int stuff;
};

int _tmain( int argc,_TCHAR * argv [])
{
Foo f1( 10 XYZ);
Foo f2( 10 xyz );
cout<< ((f1 == f2)? equal 不等于)<< ENDL;
return 0 ;
}



但是,如果我改变了Foo类,那么这对现在包括我的两个类:

  #include     stdafx.h 
#include < span class =code-keyword>< string >
#include < iostream >

使用 命名空间标准;

class Bar
{
public
Bar();
Bar(字符串,字符串);
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>数据;
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 不等于)<< ENDL;
return 0 ;
}



它不会编译而且我得到:

 1> ---- -  Build build:Project:pair_test,Configuration:Debug Win32 ------ 
1> pair_test.cpp
1> c:\program files\microsoft visual studio 10.0 \vc\include\utility(305):错误C2678:二进制'==':找不到左边的操作符'const Bar'类型的手操作数(或没有可接受的转换)
1> c:\program files\microsoft visual studio 10.0 \vc\include\exception(470):可能是'bool std :: operator ==(const std :: _ Exception_ptr&,const std :: _ Exception_ptr& ;)'
1> c:\program files\microsoft visual studio 10.0 \vc\include\exception(475):或'bool std :: operator ==(std :: _ Null_type,const std :: _ Exception_ptr&)'
1> c:\program files\microsoft visual studio 10.0 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b 1> c:\program files\microsoft visual studio 10.0 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ )'
1> c:\program files\microsoft visual studio 10.0 \vc\include\system_error(416):或'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):或'const bool Bar :: operator ==(const Bar&) '
1>在尝试匹配参数列表'(const Bar,const Bar)'
1> c:\documents and settings \charles \ my documents \visual studio 2010 \projects\pair_test\pair_test.cpp(24):参见函数模板实例化'bool std :: operator ==< Bar,Bar>(const std :: pair< _Ty1,_Ty2>&,const std :: pair< _Ty1,_Ty2>&)'正在编译
1>
1> [
1> _Ty1 = Bar,
1> _Ty2 = Bar
1> ]
1> c:\program files \ microsoft visual studio 10.0 \vc\include\utility(305):错误C2678:二进制'==':找不到左侧的操作符'const Bar'类型的操作数(或没有可接受的转换)
1> c:\program files\microsoft visual studio 10.0 \vc\include\exception(470):可能是'bool std :: operator ==(const std :: _ Exception_ptr&,const std :: _ Exception_ptr& ;)'
1> c:\program files\microsoft visual studio 10.0 \vc\include\exception(475):或'bool std :: operator ==(std :: _ Null_type,const std :: _ Exception_ptr&)'
1> c:\program files\microsoft visual studio 10.0 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b 1> c:\program files\microsoft visual studio 10.0 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ )'
1> c:\program files\microsoft visual studio 10.0 \vc\include\system_error(416):或'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):或'const bool Bar :: operator ==(const Bar&) '
1>尝试匹配参数列表'(const Bar,const Bar)'



看起来我在这里遗漏了一些明显的东西,但搜索并没有多大帮助。我是否必须为pair< bar,>?明确定义==运算符?如果它知道如何比较Bar对象,那么泛型==运算符应该正常工作,我认为。

解决方案

你的比较运算符没有定义为const,而是Foo比较operator提供const参数。因此找不到匹配项。试试这个:

  class  Bar 
{
public
...
bool const operator ==( const Bar& b) const {返回 b.s1 == s1&& b.s2 == s2;
// 在这里观看这个添加的const ...... ^
};
class Foo
{
public
。 ..
bool const operator ==( const Foo& f) const { return f.data == data; };



注意返回类型后的 const 是返回类型的限定符,而不是函数!


比较对模板的对象

  private 
pair< Bar,Bar>数据;





in

  bool  < span class =code-keyword> const   operator  ==( const  Foo& f ){ return  f.data == data; }; 





毫无意义。你需要比较那对中的每个成员。也许这适合你:

  bool   const   operator  ==( const  Foo& f){返回(f.data.first == data.first)&& (f.data.second == data.second); }; 


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.

解决方案

Your comparison operators are not defined const, but the Foo comparison operator provides const arguments. Therefore no match is found. Try this:

class Bar
{
public:
    ...
    bool const operator==(const Bar& b) const { return b.s1 == s1 && b.s2 == s2;
    //  watch this added const here ......^
};
class Foo
{
public:
	...
	bool const operator==(const Foo& f) const { return f.data == data ; };


Note that the const after the return type is a qualifier to the return type, not to the function!


you compare the object of the pair template

private:
    pair<Bar, Bar> data;



in

bool const operator==(const Foo& f) { return f.data == data ; };



that makes no sense. You got to compare each member of that pair. Maybe that fits for you:

bool const operator==(const Foo& f) { return (f.data.first == data.first) && (f.data.second == data.second) ; };


这篇关于比较对象对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆