A problem with overloading << (compiler = djgpp)

T

Tatu Portin

1: #include <iostream>
2:
3: typedef struct {
4: double r;
5: double i;
6: } complex;
..
..
..
24: ostream & operator<< (ostream &str, const complex &a)
25: {
26: s << "(" << a.r << " + i" << a.i ")";
27: return s;
28: }

when compiled with gcc:
gcc complex.cc
complex.cc:24: syntax error before `&' token

I think this error has something to do with that 'ostream'. I have #included
<iostream>, so that should not cause this?
 
J

John Harrison

Tatu Portin said:
1: #include <iostream>
2:
3: typedef struct {
4: double r;
5: double i;
6: } complex;

That is C style code, its not wrong but in C++ we prefer

struct complex {
double r;
double i;
};
.
.
.
24: ostream & operator<< (ostream &str, const complex &a)
25: {
26: s << "(" << a.r << " + i" << a.i ")";
27: return s;
28: }

when compiled with gcc:

complex.cc:24: syntax error before `&' token

I think this error has something to do with that 'ostream'. I have
#included <iostream>, so that should not cause this?

You are forgetting that ostream is in the std namespace.

std::eek:stream & operator<< (std::eek:stream &str, const complex &a)

Also look at

using std::eek:stream;

or

using namespace std;

John
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top