Reputation: 143
I was on here earlier to get some information on overloading *=
and found it very helpful.
I am now trying to overload ==
and !=
. But I want to make the operator functions non-member functions.
Try not to hate all my code blocks - I have included what I believe to be relevant to the issues.
In my source code, I declare the functions:
bool operator==(const Statistician&, const Statistician&);
// checks whether Stat1 == Stat2
bool operator!=(const Statistician&, const Statistician&);
// checks whether Stat1 != Stat2
Later on I define the functions:
// 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;
}
In my test driver, I do the actual testing:
if (Stat1 == Stat2) { cout << "Equal"; }
if (Stat1 != Stat2) { cout << "Not Equal"; }
The rest of my code works fine, and these are again not class member functions.
But when I compile I get these errors:
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)'
After much internet research I still don't know what those errors mean.
Upvotes: 2
Views: 7236
Reputation: 56863
First of all, your operator!=
is wrong, it should be
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;
(note ||
instead of &&
). Better yet, make that
return !(Stat1==Stat2);
Now for the error you see: It looks like you forgot to include the header which declares these operators in the translation unit where you are using them. Or, if that is not the case, you need to check which namespaces they are in and if they can be found there from where they are called.
Just to check: If you say "In my source code, I declare the functions:" you mean the header-file (*.h
) and with "later on" you mean the implementation file (*.cc
), right?
Upvotes: 7