473,488 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

overloading operator<< as global friend funcion

Hi

I want to overload the operator<< for a class Vector.
class Vector {
double x;
double y;
double z;

public:
Vector( double X, double Y, double Z) {
x = X;
y = Y;
z = Z;
};

friend ostream & operator<<( ostream & os, const Vector& v);

}

The operator function is defined as

ostream & operator<<( ostream & os, const Vector & v)
{
os << v.x << " " << v.y << " " << v.z << endl;
return os;
}

in global scope. The function use the private members of the Vector calss.
That's way I declared the function as friend.

The problem is that it doesn't work and the compiler(VC++6) says that the
function cannot access the private members x, y, z.

I'm a newbie but shouldn't the friend keyword solve this problem? Also the
code is equal to samples I've found in books.

The code works fine if I change the function name "operator<<" to some thing
normal e. g. "func". But this is no operator overloading.
thanks for help


Jul 22 '05 #1
3 2013

"Robert Wierschke" <wi****@gmx.de> wrote in message
news:ce*************@news.t-online.com...
Hi

I want to overload the operator<< for a class Vector.
class Vector {
double x;
double y;
double z;

public:
Vector( double X, double Y, double Z) {
x = X;
y = Y;
z = Z; };
The above semicolon is not required.

friend ostream & operator<<( ostream & os, const Vector& v);

} ;
A semicolon is required here.
The operator function is defined as

ostream & operator<<( ostream & os, const Vector & v)
{
os << v.x << " " << v.y << " " << v.z << endl;
return os;
}


Seems like problem with VC 6. It compiles fine with VC 7.0

-Sharad
Jul 22 '05 #2
On Sat, 31 Jul 2004 15:27:30 +0200, Robert Wierschke <wi****@gmx.de> wrote:
Hi

I want to overload the operator<< for a class Vector.
class Vector {
double x;
double y;
double z;

public:
Vector( double X, double Y, double Z) {
x = X;
y = Y;
z = Z;
};

friend ostream & operator<<( ostream & os, const Vector& v);

}

The operator function is defined as

ostream & operator<<( ostream & os, const Vector & v)
{
os << v.x << " " << v.y << " " << v.z << endl;
return os;
}

in global scope. The function use the private members of the Vector
calss.
That's way I declared the function as friend.

The problem is that it doesn't work and the compiler(VC++6) says that the
function cannot access the private members x, y, z.

I'm a newbie but shouldn't the friend keyword solve this problem? Also
the
code is equal to samples I've found in books.

The code works fine if I change the function name "operator<<" to some
thing
normal e. g. "func". But this is no operator overloading.
thanks for help


It's a known bug in VC++ 6. I believe you can fix it by downloading the
latest service pack (SP5 I think). Also I think you can fix it by defining
the function in the class, i.e.

class Vector {
...
friend ostream & operator<<(ostream & os, const Vector& v)
{
// code here
}
};

However you should consider upgrading to a beter compiler than VC++ 6.
It's along way from being a correct implementation of the C++ language.

You could consider VC++ 7.1 or gcc 3.3, both are free.

john
Jul 22 '05 #3
I was having the same problem tonight. What I had to do was to make a
forward declaration of the friend function.

//--Forward declaration
ostream & operator<<( ostream & os, const Vector& v);

//--Now define your class
class Vector
{
-------
-------

public:
friend ostream & operator << ( ostream & os,
const Vector& v );
}; //--End of class Vector

//--Now define your operator function, which can be put
//--at the end of your class declaration in the
//--at the header file or in the .cpp file.

ostream & operator<<( ostream & os, const Vector& v)
{
----------------;
}
Louise

Jul 22 '05 #4

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

Similar topics

10
1608
by: pmatos | last post by:
Hi all, I have the following code: class test { public: test(const std::string *n) : name(n) {} virtual ~test() {} const std::string * getName() { return name; }
3
1313
by: pmatos | last post by:
Hi all, I'm having a problem and for illustration purposes I developed code that shows what the problem is about. However, any comment on the code which is not directly about this issue is...
8
8353
by: jois.de.vivre | last post by:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code: #include <iostream> using namespace std; class test { private: int val;
0
900
by: Marco Segurini | last post by:
Hi, I like to know why the compiler returns me an error if I implement operator<< for managed types as free function. //-------- #include <iostream> using namespace System;
1
2110
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
4
2136
by: CrimzonRJ | last post by:
Theoretically, how are you supposed to overload << as friend to a template class? I keep getting an 'unresolved extern symbol' error. Is this even a smart thing to do?
7
2772
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
6
1478
by: Peter v. N. | last post by:
Hi all, Maybe this has been asked a million times before. In that case I'm sorry for being to lazy to search the Internet or look it up in a decent C++ reference: I read in O'Reilly's C++...
4
1868
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
0
7108
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
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7181
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...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7352
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...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.