Connecting Tech Pros Worldwide Forums | Help | Site Map

friends, namespaces and operators.

glen stark
Guest
 
Posts: n/a
#1: Nov 14 '05
Hi.

I had an interesting experience with a brand x compiler recently. I had
class defined within a namespace, and in that class I declared the "<<"
operator as a friend to that class. Then, in the cpp code I implemented
the operator overloading within a "using namespace" context, only to get
access errers. To get it to work I had to put a namespaceName:: in
front of the operator keyword...

for your edification, here is a sample code:
================
HEADER FILE

#include <string>
#include <ostream>

namespace voxel{
class PrivateClass{
public:
PrivateClass():info("hiya"){};
friend std::ostream& operator<<(std::ostream&, const PrivateClass&);
private:
std::string info;
};
}
===================
CPP FILE

#include "stdafx.h"
#include "PrivateClass.h"
using namespace voxel;
std::ostream& voxel::operator<<(std::ostream& os, const PrivateClass& pc)
{
os<<pc.info;
return os;
}

int main(int argc, char* argv[])
{

PrivateClass pc;
std::cout<<pc;
return 0;

}


=====end of code

So my question is: is this a compiler bug, or is this a loophole in the
standard somehow? If it's a compiler bug, does the standard imply that
it should behave as i would think, or what? I would very much like any
clarification you can offer me.

Joona I Palaste
Guest
 
Posts: n/a
#2: Nov 14 '05

re: friends, namespaces and operators.


glen stark <mail@nospam.glenstark.org> scribbled the following:[color=blue]
> Hi.[/color]
[color=blue]
> I had an interesting experience with a brand x compiler recently. I had
> class defined within a namespace, and in that class I declared the "<<"[/color]

comp.lang.c++ is that way =============================================>

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon
glen stark
Guest
 
Posts: n/a
#3: Nov 14 '05

re: friends, namespaces and operators.


Joona I Palaste wrote:
[color=blue]
> glen stark <mail@nospam.glenstark.org> scribbled the following:
>[color=green]
>>Hi.[/color]
>
>[color=green]
>>I had an interesting experience with a brand x compiler recently. I had
>>class defined within a namespace, and in that class I declared the "<<"[/color]
>
>
> comp.lang.c++ is that way =============================================>
>[/color]
oops, so sorry, clicked on the wrong group...
Martin Ambuhl
Guest
 
Posts: n/a
#4: Nov 14 '05

re: friends, namespaces and operators.


glen stark wrote:
[color=blue]
> Hi.
>
> I had an interesting experience with a brand x compiler recently. I had
> class defined within a namespace, and in that class I declared the "<<"
> operator as a friend to that class. [etc.][/color]

Please direct your questions about C++, a different language from C, to
news:comp.lang.c++

Closed Thread


Similar C / C++ bytes