2013-09-30 34 views
1

我刚才在这里得到一些关于重载* =的信息,并发现它非常有帮助。Overloading == and!=

我现在想重载==和!=。但是我想让运营商功能非会员功能。

尽量不要讨厌我所有的代码块 - 我已经列入了我认为与问题相关的内容。

在我的源代码,我宣布的功能:

bool operator==(const Statistician&, const Statistician&); 
// checks whether Stat1 == Stat2 
bool operator!=(const Statistician&, const Statistician&); 
// checks whether Stat1 != Stat2 

后来我定义的功能:

// Check if equal 
bool operator ==(const Statistician& Stat1, const Statistician& Stat2) 
{ 
if ((Stat1.get_length() == Stat2.get_length()) && 
    (Stat1.get_sum() == Stat2.get_sum()) && 
    (Stat1.get_mean() == Stat2.get_mean()) && 
    (Stat1.get_minimum() == Stat2.get_minimum()) && 
    (Stat1.get_maximum() == Stat2.get_maximum())) 
    return true; 
else 
    return false; 
} 

// Check if not equal 
bool operator !=(const Statistician& Stat1, const Statistician& Stat2) 
{ 
if ((Stat1.get_length() != Stat2.get_length()) && 
    (Stat1.get_sum() != Stat2.get_sum()) && 
    (Stat1.get_mean() != Stat2.get_mean()) && 
    (Stat1.get_minimum() != Stat2.get_minimum()) && 
    (Stat1.get_maximum() != Stat2.get_maximum())) 
    return true; 
else 
    return false; 
} 

在我的测试车手,我做实际测试:

if (Stat1 == Stat2) {cout << "Equal";} 
if (Stat1 != Stat2) {cout << "Not Equal";} 

我的代码的其余部分工作正常,这些都不是类成员函数。

但是我编译时得到这些错误:

error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Statistician' (or there is no acceptable conversion) 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(507): could be 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(512): or  'bool std::operator ==(std::nullptr_t,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(517): or  'bool std::operator ==(const std::exception_ptr &,std::nullptr_t)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(426): or  'bool std::operator ==(const std::error_code &,const std::error_condition &) throw()' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(434): or  'bool std::operator ==(const std::error_condition &,const std::error_code &) throw()' 
     while trying to match the argument list '(Statistician, Statistician)' 

error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'Statistician' (or there is no acceptable conversion) 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(522): could be 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(527): or  'bool std::operator !=(std::nullptr_t,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(532): or  'bool std::operator !=(const std::exception_ptr &,std::nullptr_t)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(443): or  'bool std::operator !=(const std::error_code &,const std::error_condition &) throw()' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(450): or  'bool std::operator !=(const std::error_condition &,const std::error_code &) throw()' 
     while trying to match the argument list '(Statistician, Statistician)' 

很多互联网研究后,我仍然不知道这些错误的意思。

帮助?谢谢!

+1

在您第一次使用它们之前,您是否声明了这些操作员? – us2012

+3

请注意,你不需要做'如果(某些)返回true;否则返回false;',你可以说'return something;'。 – juanchopanza

回答

5

首先,你operator!=是错误的,它应该是

if ((Stat1.get_length() != Stat2.get_length()) || 
    (Stat1.get_sum() != Stat2.get_sum()) || 
    (Stat1.get_mean() != Stat2.get_mean()) || 
    (Stat1.get_minimum() != Stat2.get_minimum()) || 
    (Stat1.get_maximum() != Stat2.get_maximum())) 
    return true; 
else 
    return false; 

(注意:||而不是&&)。更重要的是,使该

return !(Stat1==Stat2); 

现在你看到的错误:它看起来像你忘了包括在您使用它们的翻译单元声明这些运营商的头。或者,如果情况并非如此,您需要检查它们所在的名称空间,以及它们是否可以从被调用的位置找到它们。

只是为了检查:如果你说“在我的源代码中,我声明了这些函数:”你的意思是头文件(*.h)和“稍后”是指实现文件(*.cc),对不对?

+0

+1还将'operator =='的定义简化为'return(Stat1.get_length()== Stat2.get_length())&& ...;'。就个人而言,我会让运营商的朋友,并使用'std :: tie'来执行比较。 – Praetorian

+1

@Praetorian对于有经验的开发人员,你是对的,但鉴于OP显然刚刚开始了解运营商,让我们一步一步,不要因为太多的信息而杀死她:) –

+0

因此,对于那个错误,我有函数声明以及相同.cpp文件中的定义。所以不应该有这个问题。 它说,没有哪个运营商需要统计员为左手输入,然而当我宣布我的功能声明为 (统计员和,统计员和) 所以我很失去了现在。 – 404Cat