473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 4059

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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.