friend declaration or operator overload function cross namespace.

Discussion in 'C++' started by Layton, Sep 27, 2006.

  1. Layton

    Layton Guest

    Hi, CPP gurus,

    How to use friend function cross the namespace? I have the following
    sample code with operator << overloaded, it's working. The problem the
    operator << function can't access private class (myPoint) member x and
    y directly and I have to use getters to make it work.

    If there is no namespace, direct access myPoint::x and y is ok. I
    tested successfully by add friend declaration inside class myPoint.
    (turn on line CCC, BBB, turn off AAA). But with the extra namespace
    space1, I could not make it to work any way. Hope you can shed some
    light on this issue. Thanks in advace.

    //====================================================
    //mypoint.h
    //====================================================
    #ifndef MYPOINT_H
    #define MYPOINT_H
    #include <iostream>
    using namespace std;
    namespace space1{
    class myPoint{
    //friend std::eek:stream& operator<< (std::eek:stream& _os, const
    space1::myPoint& _a); //LINE CCC
    double x, y;
    public:
    myPoint(double _x=0, double _y=0);
    double getx() const;
    double gety() const;
    }; //end class myPoint
    }; //end namespace space1
    std::eek:stream& operator<< (std::eek:stream& _os, const space1::myPoint&
    _a);
    #endif

    //====================================================
    //mypoint.cpp
    //====================================================
    #include "mypoint.h"
    using namespace std;
    using namespace space1;

    myPoint::myPoint(double _x, double _y):x(_x),y(_y){}
    double myPoint::getx() const {return x;}
    double myPoint::gety() const {return y;}
    std::eek:stream& operator<< (std::eek:stream& _os, const space1::myPoint&
    _a){
    _os<<"("<<_a.getx()<<", "<<_a.gety()<<")"<<endl; //LINE
    AAA
    //_os<<"[("<<_a.x<<", "<<_a.y<<")]"<<endl; //LINE
    BBB
    return _os;
    }


    //====================================================
    //main.cpp
    //====================================================
    #include "mypoint.h"
    #include <iostream>
    using namespace std;
    using namespace space1;

    int main(){
    myPoint m1(1,2), m2, m3;
    myPoint n(3,2);
    m2=n; //test assignment oper
    cout<<"HELLO"<<endl;
    cout<<m1<<m2<<m3; //m3 test default constr
    return 0;
    }
     
    Layton, Sep 27, 2006
    #1
    1. Advertising

  2. Layton wrote:
    > Hi, CPP gurus,
    >
    > How to use friend function cross the namespace? I have the following
    > sample code with operator << overloaded, it's working. The problem
    > the operator << function can't access private class (myPoint) member
    > x and y directly and I have to use getters to make it work.
    >
    > If there is no namespace, direct access myPoint::x and y is ok. I
    > tested successfully by add friend declaration inside class myPoint.
    > (turn on line CCC, BBB, turn off AAA). But with the extra namespace
    > space1, I could not make it to work any way. Hope you can shed some
    > light on this issue. Thanks in advace.
    >
    > //====================================================
    > //mypoint.h
    > //====================================================
    > #ifndef MYPOINT_H
    > #define MYPOINT_H
    > #include <iostream>
    > using namespace std;


    Add these lines here:

    namespace space1 { class myPoint; }
    std::eek:stream& operator<< (std::eek:stream&, space1::myPoint const&);

    > namespace space1{
    > class myPoint{
    > //friend std::eek:stream& operator<< (std::eek:stream& _os, const
    > space1::myPoint& _a); //LINE CCC


    If you want this line to declare the _global_ operator<< a friend,
    you need to declare that function before the namespace (see above)

    > double x, y;
    > public:
    > myPoint(double _x=0, double _y=0);
    > double getx() const;
    > double gety() const;
    > }; //end class myPoint
    > }; //end namespace space1


    Drop the semicolon after the closing brace for the namespace.

    > std::eek:stream& operator<< (std::eek:stream& _os, const space1::myPoint&
    > _a);


    You need to move this declaration above the class (see my "add"
    comment above).

    > #endif
    > [...]


    I think it will work after those changes. I didn't check, though,
    so please post back with the results.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 27, 2006
    #2
    1. Advertising

  3. Layton

    Layton Guest

    Hi, Victor, Thanks for your posting.


    it turned out the key is the overloaded operator << can be of space std
    or space1. As long as the code make clear the space operator <<
    belonging to, the code works right away.

    So the working code follows. (turn on Line BBB, CCC, turn off AAA, add
    "space1::" on line DDD and EEE. That's it.)


    //====================================================
    //mypoint.h
    //====================================================
    #ifndef MYPOINT_H
    #define MYPOINT_H
    #include <iostream>
    using namespace std;
    namespace space1{
    class myPoint{
    friend std::eek:stream& operator<< (std::eek:stream& _os, const
    space1::myPoint& _a); //LINE CCC
    double x, y;
    public:
    myPoint(double _x=0, double _y=0);
    double getx() const;
    double gety() const;
    }; //end class myPoint
    }; //end namespace space1
    std::eek:stream& space1::eek:perator<< (std::eek:stream& _os, const
    space1::myPoint& _a); //LINE DDD
    #endif

    //====================================================
    //mypoint.cpp
    //====================================================
    #include "mypoint.h"
    using namespace std;
    using namespace space1;

    myPoint::myPoint(double _x, double _y):x(_x),y(_y){}
    double myPoint::getx() const {return x;}
    double myPoint::gety() const {return y;}
    std::eek:stream& space1::eek:perator<< (std::eek:stream& _os, const
    space1::myPoint& _a){//LINE EEE
    //_os<<"("<<_a.getx()<<", "<<_a.gety()<<")"<<endl; //LINE
    AAA
    _os<<"[("<<_a.x<<", "<<_a.y<<")]"<<endl; //LINE BBB
    return _os;
    }


    //====================================================
    //main.cpp
    //====================================================
    #include "mypoint.h"
    #include <iostream>
    using namespace std;
    using namespace space1;

    int main(){
    myPoint m1(1,2), m2, m3;
    myPoint n(3,2);
    m2=n; //test assignment oper
    cout<<"HELLO"<<endl;
    cout<<m1<<m2<<m3; //m3 test default constr
    return 0;
    }
     
    Layton, Sep 27, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yu Lianqing
    Replies:
    1
    Views:
    666
    Leor Zolman
    Apr 4, 2004
  2. Gianni Mariani
    Replies:
    5
    Views:
    1,020
    Jerry Coffin
    Jan 18, 2005
  3. Teddy
    Replies:
    5
    Views:
    9,674
    Swampmonster
    Jun 10, 2005
  4. Mahain
    Replies:
    3
    Views:
    1,139
    James Kanze
    Dec 20, 2007
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    281
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page