operator overloading error
Code:
#include <iostream>
using std::cout;
using std::endl;
class CEstimatedInteger
{
private:
int value;
bool estimated;
public:
// Constructor
CEstimatedInteger(int val = 0, bool est = false) : value(val), estimated(est) { }
// Function to print value and estimation flag
void Print()
{
if (estimated)
cout << 'E';
cout << value;
}
// Addition operator for operands CEstimatedInteger + int
CEstimatedInteger operator+(const int& n)
{
return CEstimatedInteger(value + n, estimated);
}
// Addition operator for operands CEstimatedInteger + CEstimatedInteger
CEstimatedInteger operator+(const CEstimatedInteger& n)
{
return CEstimatedInteger(value + n.value, estimated || n.estimated);
}
};
CEstimatedInteger operator+(const int& a, const CEstimatedInteger& b)
{
return b + a;
}
int main()
{
CEstimatedInteger a;
return EXIT_SUCCESS;
}
Whats wrong with the above code?
I'm getting the following errors while compiling in Visual Studio 2008
Code:
1>------ Build started: Project: C++Test, Configuration: Debug Win32 ------
1>Compiling...
1>Test.cpp
1>c:\documents and settings\administrator\my documents\visual studio 2005\projects\c++test\c++test\test.cpp(40) : error C2678: binary '+' : no operator found which takes a left-hand operand of type 'const CEstimatedInteger' (or there is no acceptable conversion)
1> c:\documents and settings\administrator\my documents\visual studio 2005\projects\c++test\c++test\test.cpp(38): could be 'CEstimatedInteger operator +(const int &,const CEstimatedInteger &)'
1> c:\documents and settings\administrator\my documents\visual studio 2005\projects\c++test\c++test\test.cpp(26): or 'CEstimatedInteger CEstimatedInteger::operator +(const int &)'
1> c:\documents and settings\administrator\my documents\visual studio 2005\projects\c++test\c++test\test.cpp(32): or 'CEstimatedInteger CEstimatedInteger::operator +(const CEstimatedInteger &)'
1> while trying to match the argument list '(const CEstimatedInteger, const int)'
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\C++Test\C++Test\Debug\BuildLog.htm"
1>C++Test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========