friend declaration or operator overload function cross namespace.

L

Layton

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;
}
 
V

Victor Bazarov

Layton said:
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
 
L

Layton

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top