Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   about operator overloading (http://www.go4expert.com/forums/about-operator-overloading-t7603/)

T.C 28Nov2007 16:58

about operator overloading
 
Hi there, I think this my first time :)

For while I was working in learning Operator overloading but I still canít figure it out.
I did try several times by using different codes from various materials but still!!
And this is one of my trial which doesn't work, so please if you donít mind giving some tips on dealing with operator overloading and tell me what is the wrong with the code;!

note: I did a search in the site about the issue before request my demand.

Code:

#include <iostream>
using namespace std;

using std::ostream;
class Number{

        friend ostream &operator<<(ostream &, const Number &);
public:
        Number();
        void setN(int);
        int getN()const;

        Number operator+(const Number &)const;

private:
        int number;
};

Number::Number(){number=0;}
void Number::setN(int n){number=n;}
int Number::getN()const {return number;}

ostream &operator<<(ostream &output, const Number &num)
{
        output<<num.number;
        return output;
}

Number Number::operator +(const Number &other)const
{
        return  *this + Number(other);
}

int main()
{
        Number n1,n2;
        Number sum;
       
       
        n1.setN(3);
        n2.setN(4);
        cout<<": "<<n1.getN()<<endl;
        cout<<": "<<n2.getN()<<endl;
        sum= n1+n2;
              cout<<sum<<endl;
       
        return 0;
}

errors I got:
1.cannot access private member declared in class 'Number'
2. see declaration of 'number'
3. operator <<' is ambiguous

shabbir 28Nov2007 17:24

Re: about operator overloading
 
Your << operator is a private function because class default specifier is private by default.


All times are GMT +5.5. The time now is 00:11.