By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

friend declaration or operator overload function cross namespace.

P: n/a
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::myPoint(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.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;
}

Sep 27 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Sep 27 '06 #2

P: n/a
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::operator<< (std::ostream& _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::ostream& space1::operator<< (std::ostream& _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;
}

Sep 27 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.