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:stream& operator<< (std: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:stream& operator<< (std: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:stream& operator<< (std: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;
}
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:stream& operator<< (std: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:stream& operator<< (std: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:stream& operator<< (std: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;
}