473,395 Members | 1,956 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,395 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.
Nov 14 '05 #1
3 1237
glen stark <ma**@nospam.glenstark.org> scribbled the following:
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 "<<"


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon
Nov 14 '05 #2
Joona I Palaste wrote:
glen stark <ma**@nospam.glenstark.org> scribbled the following:
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 "<<"

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

oops, so sorry, clicked on the wrong group...
Nov 14 '05 #3
glen stark wrote:
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++

Nov 14 '05 #4

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...
2
by: glen stark | last post by:
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.