friends, namespaces and operators.

G

glen stark

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::eek:stream& operator<<(std::eek:stream&, const PrivateClass&);
private:
std::string info;
};
}
===================
CPP FILE

#include "stdafx.h"
#include "PrivateClass.h"
using namespace voxel;
std::eek:stream& voxel::eek:perator<<(std::eek:stream& 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.
 
J

Joona I Palaste

glen stark said:
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 "<<"

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

Martin Ambuhl

glen said:
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.]

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

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top