473,833 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ostream overloading

Hi there,

I tried with Visual C++ 6.0 to overloading the ostream operator for an
object. While compiling I receive this error:

Compiling...
main.cpp
C:\[...]\main.cpp(8) : error C2678: binary '<<' : no operator defined which
takes a left-hand operand of type 'class ostream_withass ign' (or there is no
acceptable conversion)

If someone knows way I would appreciate an help.

Thanks, Banshee

here there is the code :
-----------VERTEX.H----------------------
#include "stdafx.h"
#include <math.h>
#include <vector>
#include <iostream.h>
#include <string>
class ClVertex
{
public:

ClVertex ()
{
this->SetZero ();
}

ClVertex(float vx, float vy, float vz)
{
n.x = vx;
n.y = vy;
n.z = vz;
}
ClVertex( float f )
{
n.x = f;
n.y = f;
n.z = f;
}

~ClVertex()
{}

/** Get the x component.
*/
float get_x() const
{
return ( n.x );
}
/** Set the x component.
*/
void set_x( float v_x )
{
n.x = v_x;
}
/** Get the y component.
*/
float get_y() const
{
return ( n.y );
}
/** Set the y component.
*/
void set_y( float v_y )
{
n.y = v_y;
}
/** Get the z component.
*/
float get_z() const
{
return ( n.z );
}
/** Set the z component.
*/
void set_z( float v_z )
{
n.z = v_z;
}

/** Array based component access.
* \param i Integer component index, 0-2.
* \return Appropriate component, or z if index is invalid.
*/
float& operator[] (const int idx)
{
return a[idx];
}
ClVertex operator - (const ClVertex &v)
{
return ClVertex(n.x-v.n.x, n.y-v.n.y, n.z-v.n.z);
};
ClVertex& operator = (const ClVertex *vect)
{
n.x = vect->n.x;
n.y = vect->n.x;
n.z = vect->n.x;
return *this;
};
friend std::ostream &operator<<( std::ostream &Stream, const ClVertex
&V ) ;

void SetZero ()
{
n.x = n.y = n.z = 0;
};

protected:

union
{
struct
{
float x, y, z;
} n;
float a[3];
};
};

-----------VERTEX.CPP----------------------
#include "stdafx.h"
#include "vertex.h"

std::ostream &operator<<( std::ostream &Stream, const ClVertex &V )
{
Stream << V.n.x << "," << V.n.y << "," << V.n.z;
return ( Stream );
}
-----------MAIN.CPP--------------------
#include "stdafx.h"
#include "vertex.h"

int main()
{

ClVertex v(1, 2, 3);
cout<<v;

return 0;
}

Jul 23 '05 #1
5 4085

Banshee wrote:
Hi there,

I tried with Visual C++ 6.0 to overloading the ostream operator for an object. While compiling I receive this error:

Compiling...
main.cpp
C:\[...]\main.cpp(8) : error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class ostream_withass ign' (or there is no acceptable conversion)

If someone knows way I would appreciate an help.

Thanks, Banshee

here there is the code :
-----------VERTEX.H----------------------
#include "stdafx.h"
#include <math.h>
#include <vector>
#include <iostream.h>
#include <string>
class ClVertex
{
public:

ClVertex ()
{
this->SetZero ();
}

ClVertex(float vx, float vy, float vz)
{
n.x = vx;
n.y = vy;
n.z = vz;
}
ClVertex( float f )
{
n.x = f;
n.y = f;
n.z = f;
}

~ClVertex()
{}

/** Get the x component.
*/
float get_x() const
{
return ( n.x );
}
/** Set the x component.
*/
void set_x( float v_x )
{
n.x = v_x;
}
/** Get the y component.
*/
float get_y() const
{
return ( n.y );
}
/** Set the y component.
*/
void set_y( float v_y )
{
n.y = v_y;
}
/** Get the z component.
*/
float get_z() const
{
return ( n.z );
}
/** Set the z component.
*/
void set_z( float v_z )
{
n.z = v_z;
}

/** Array based component access.
* \param i Integer component index, 0-2.
* \return Appropriate component, or z if index is invalid.
*/
float& operator[] (const int idx)
{
return a[idx];
}
ClVertex operator - (const ClVertex &v)
{
return ClVertex(n.x-v.n.x, n.y-v.n.y, n.z-v.n.z);
};
ClVertex& operator = (const ClVertex *vect)
{
n.x = vect->n.x;
n.y = vect->n.x;
n.z = vect->n.x;
return *this;
};
friend std::ostream &operator<<( std::ostream &Stream, const ClVertex &V ) ;

void SetZero ()
{
n.x = n.y = n.z = 0;
};

protected:

union
{
struct
{
float x, y, z;
} n;
float a[3];
};
};

-----------VERTEX.CPP----------------------
#include "stdafx.h"
#include "vertex.h"

std::ostream &operator<<( std::ostream &Stream, const ClVertex &V )
{
Stream << V.n.x << "," << V.n.y << "," << V.n.z;
return ( Stream );
}
-----------MAIN.CPP--------------------
#include "stdafx.h"
#include "vertex.h"

int main()
{

ClVertex v(1, 2, 3);
cout<<v;

return 0;
}


Try adding:

std::ostream &operator<<( std::ostream &Stream, const ClVertex &V );

At the end of vertex.h (after your class definition). See if that
works.

-shez-

Jul 23 '05 #2

"Banshee" <ba***********@ tiscali.it> wrote in message
news:41******** ************@ne ws.tiscali.it.. .
Hi there,

I tried with Visual C++ 6.0 to overloading the ostream operator for an
object. While compiling I receive this error:

Compiling...
main.cpp
C:\[...]\main.cpp(8) : error C2678: binary '<<' : no operator defined which takes a left-hand operand of type 'class ostream_withass ign' (or there is no acceptable conversion)

If someone knows way I would appreciate an help.

Thanks, Banshee

here there is the code :
-----------VERTEX.H----------------------
#include "stdafx.h"
#include <math.h>
#include <vector>
#include <iostream.h>


This is non-standard ... use <iostream> instead. I guess that is where the
problems may be coming from, but I don't have experience with VC++. Your
code looks correct otherwise.

You can always try to paste it into Comeau online (google it) to get the
opinion of another (more standard compliant) compiler.

HTH,

Dave Moore
Jul 23 '05 #3
I tried to replace #include <iostream.h> with #include <iostream>. And then
I add to vertex.h "using namespace std;".

It works!!!!

Thanks anyway, bye.

Banshee
"Banshee" <ba***********@ tiscali.it> ha scritto nel messaggio
news:41******** ************@ne ws.tiscali.it.. .
Hi there,

I tried with Visual C++ 6.0 to overloading the ostream operator for an
object. While compiling I receive this error:

Jul 23 '05 #4
Banshee wrote:

I tried to replace #include <iostream.h> with #include <iostream>. And then
I add to vertex.h "using namespace std;".


That's not a good idea.
In header files you should refrain from using this 'using' construct.
Otherwise everybody including your header gets that 'using' too and
you don't know if he wants that to happen.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #5
"Banshee" <ba***********@ tiscali.it> wrote in message news:<41******* *************@n ews.tiscali.it> ...
If someone knows way I would appreciate an help.

Thanks, Banshee

here there is the code :
-----------VERTEX.H----------------------
#include "stdafx.h"
#include <math.h>
#include <vector>
#include <iostream.h>
#include <string>
[...]

-----------MAIN.CPP--------------------
#include "stdafx.h"
#include "vertex.h"

int main()
{

ClVertex v(1, 2, 3);
cout<<v;

return 0;
}


Well, you could try using the standard library IOStreams instead.

Use the header

#include <iostream>

and in main(), use

std::cout << v;

You may find more satisfaction.

--
Stephen M. Webb
Jul 23 '05 #6

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

Similar topics

3
8271
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it at all possible?
3
7833
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another for the screen. Right now the two declarations are: std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass); std::ostream& operator<<( std::ostream &fos, const Foo &theClass);
1
1443
by: sahm | last post by:
hi, i have to create a program that will print the 52 cards in a deck using the overloaded ostream operator. it worked fine....then i hve to create a function that will print the cards in hand in random(5, 10). //error it shld only have two parameters ostream& operator<<(ostream& out, vector<Card>& deck2, vector<Card>& hand) {
1
1567
by: sahm | last post by:
hi, i created a function ostream& operator<<(ostream& out, vector<Card>& deck) { int i = 0; for (int s = 1; s <= 4; s++) { for (int r = 1; r <= 13; r++, i++) { deck.setSuit(s);
4
6749
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if it caught anything, it's very picky... Anyway, the code below works on Dev, and it compiles fine on Borland, but when I run it from borland, there is no output, no error, it just skips right over the freind ostream call. HELP! I'm new to this...
10
9672
by: Johannes Barop | last post by:
Hi, I want to format the output of a 'std::ostream', but i dont know how to do it . Example: int main() { my_out << "Hi.\nI'am a" << " Computer."; my_out << "Nice.";
2
2959
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
6
4731
by: syang8 | last post by:
Any one can specify the problem of the following code? The compiling error is on the friend function. If the base class is not inherited from ostream, or I just remove the friend function from the base class, the code will compile. ------------------------------------------------------------------------- #include <iostream> template<class Tclass base; template<class Tstd::ostream& operator<< (std::ostream&, base<T>&);
3
2432
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line "hello world". #include <iostream> using namespace std;
0
10782
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10500
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10213
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9323
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4422
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3972
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3078
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.