473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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::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
2 2177
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
2
by: Tim Partridge | last post by:
I'm not very good with namespaces, so my problem is probably a simple one. I can't get the following to compile on gcc 3.3.1. It reports main.cc: In function 'std::ostream &...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
1
by: paidojoao-groups | last post by:
Given this code: #include <iostream> #include <string> class timestamp { public: timestamp() {
5
by: James Aguilar | last post by:
Hello, all. I saw this in the provided code of a lab that I have due in a couple of weeks in my algorithms class. It compiled fine in g++ but did not compile with the VC++ compiler. It does...
5
by: Gianni Mariani | last post by:
Can anyone enligten me why I get the "ambiguous overload" error from the code below: friendop.cpp: In function `int main()': friendop.cpp:36: ambiguous overload for `std::basic_ostream<char,...
5
by: Teddy | last post by:
Hello all consider the class Date declaretion below: class Date { public: Date(); Date(int year, int month, int day); Date(const string&); int getYear() const;
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.