Thread: operator overloading error

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    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 ==========

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since your operator + doesn't change the lhs, you should specify as much:
    Code:
    CEstimatedInteger operator+(const int& n) const

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Didn't know it's mandatory

    tY

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by manzoor View Post
    Didn't know it's mandatory

    tY
    It's only mandatory if you want to use it on a const variable (which you do in your other operator +). It's a good idea, always.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM