ostream overloading

B

Banshee

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::eek:stream &operator<<( std::eek:stream &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::eek:stream &operator<<( std::eek:stream &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;
}
 
S

shez

Banshee said:
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::eek:stream &operator<<( std::eek:stream &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::eek:stream &operator<<( std::eek:stream &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::eek:stream &operator<<( std::eek:stream &Stream, const ClVertex &V );

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

-shez-
 
D

Dave Moore

Banshee said:
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
 
B

Banshee

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
 
K

Karl Heinz Buchegger

Banshee said:
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.
 
S

Stephen M. Webb

Banshee said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top