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.


#include <iostream>
using namespace std;

using std::ostream;
class Number{

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

        Number operator+(const Number &)const;

        int number;

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

ostream &operator<<(ostream &output, const Number &num)
        return output;

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

int main()
        Number n1,n2;
        Number sum;
        cout<<": "<<n1.getN()<<endl;
        cout<<": "<<n2.getN()<<endl;
        sum= n1+n2;
        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 21:02.