473,322 Members | 1,345 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,322 software developers and data experts.

friends namespaces and operators

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.
Jul 22 '05 #1
2 2239

"glen stark" <ma**@nospam.glenstark.org> wrote in message
news:41********@pfaff2.ethz.ch...
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?
Neither, its your understanding of C++ that is wrong.
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.


You seem to be assuming that writing 'using namespace voxel;' should put
subsequent definitions in the voxel namespace but that is not the case (if
it were the case then you would have put main in the voxel namespace). Or
maybe you are thinking that the compiler should somehow make a connection
between the operator<< in your source file and the operator<< in your header
file, but as far as the compiler is concerned they are just similar
functions in different namespaces.

'Using namespace ...' affects how names are looked up, it does not affect
which namespace names are defined in. The only way to do that is this

namespace voxel
{
std::ostream& operator<<(std::ostream& os, const PrivateClass& pc)
{
...
}
}

or this

std::ostream& voxel::operator<<(std::ostream& os, const PrivateClass&
pc)
{
...
}

john
Jul 22 '05 #2
John Harrison wrote:
"glen stark" <ma**@nospam.glenstark.org> wrote in message
news:41********@pfaff2.ethz.ch...
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?

Neither, its your understanding of C++ that is wrong.

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.

You seem to be assuming that writing 'using namespace voxel;' should put
subsequent definitions in the voxel namespace but that is not the case (if
it were the case then you would have put main in the voxel namespace). Or
maybe you are thinking that the compiler should somehow make a connection
between the operator<< in your source file and the operator<< in your header
file, but as far as the compiler is concerned they are just similar
functions in different namespaces.

'Using namespace ...' affects how names are looked up, it does not affect
which namespace names are defined in. The only way to do that is this

namespace voxel
{
std::ostream& operator<<(std::ostream& os, const PrivateClass& pc)
{
...
}
}

or this

std::ostream& voxel::operator<<(std::ostream& os, const PrivateClass&
pc)
{
...
}

john

Thanks, that clears it up.

Glen
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon | last post by:
Hi, I have a class Foo which defines a friend function Bar. When I place the Bar function within a namespace, I can no longer access the Foo private data (i'm guessing it is not recognised as a...
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
4
by: masood.iqbal | last post by:
Please help me with this doubt that I have regarding overloaded operators. Sometimes they are member functions and sometimes they are friends (e.g. see the code snippet from Stroustrup, Second...
15
by: Stuart | last post by:
I work in a small company with developers who do not like to use "new" features when they find the old ones sufficient. e.g. given a choice between a class and a namespace, they'll pick class. ...
4
by: George Economos | last post by:
Hi all, I am using msvc 7.1 and have encountered the following code: ----------- A.hpp ----------- class A {
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
1
by: Alexander Bürger | last post by:
Hi, I have a question about operators. I have something similar to the following code: //================ // file a.h class A { ... }; // file op.h
3
by: Alexander Bürger | last post by:
Hi, I have a little problem with classes in a namespace and an operator defined for these classes outside that namespace. Example: //...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.