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