friend declaration or operator overload function cross namespace.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Layton

    friend declaration or operator overload function cross namespace.

    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::ostream& operator<< (std::ostream& _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::ostream& operator<< (std::ostream& _os, const space1::myPoint &
    _a);
    #endif

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

    myPoint::myPoin t(double _x, double _y):x(_x),y(_y) {}
    double myPoint::getx() const {return x;}
    double myPoint::gety() const {return y;}
    std::ostream& operator<< (std::ostream& _os, const space1::myPoint &
    _a){
    _os<<"("<<_a.ge tx()<<", "<<_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<<m 3; //m3 test default constr
    return 0;
    }

  • Victor Bazarov

    #2
    Re: friend declaration or operator overload function cross namespace.

    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::ostream& operator<< (std::ostream&, space1::myPoint const&);
    namespace space1{
    class myPoint{
    //friend std::ostream& operator<< (std::ostream& _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::ostream& operator<< (std::ostream& _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


    Comment

    • Layton

      #3
      Re: friend declaration or operator overload function cross namespace.

      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::ostream& operator<< (std::ostream& _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::ostream& space1::operato r<< (std::ostream& _os, const
      space1::myPoint & _a); //LINE DDD
      #endif

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

      myPoint::myPoin t(double _x, double _y):x(_x),y(_y) {}
      double myPoint::getx() const {return x;}
      double myPoint::gety() const {return y;}
      std::ostream& space1::operato r<< (std::ostream& _os, const
      space1::myPoint & _a){//LINE EEE
      //_os<<"("<<_a.ge tx()<<", "<<_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<<m 3; //m3 test default constr
      return 0;
      }

      Comment

      Working...