By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

ostream overloading

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.