472,357 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,357 software developers and data experts.

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_withassign' (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 4001

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_withassign' (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********************@news.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_withassign' (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********************@news.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********************@news.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
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...
3
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...
1
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...
1
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
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...
10
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
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
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...
3
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.