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

Operator Overloading

P: n/a
#ifndef ADDRESS_H
#define ADDRESS_H

class Address
{
private:
int house;
int block;
char* town;
char* city;
public:
Address(char*,int, char*, int);
friend ostream& operator<<(ostream&, const Address&);
};
#endif

ostream& operator<<(ostream& a, const Address& b)
{
a<<"House No "<<b.house<<endl;
a<<"Block Number "<<b.block<<endl;
a<<"Town "<<b.town<<endl;
a<<"City "<<b.city<<endl;
return a;
};

Can anybody plrase help me to identify the error here? It is not
letting me access the data members of the class.

May 29 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
<om***********@inbox.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com
#ifndef ADDRESS_H
#define ADDRESS_H

class Address
{
private:
int house;
int block;
char* town;
char* city;
public:
Address(char*,int, char*, int);
friend ostream& operator<<(ostream&, const Address&);
};
#endif

ostream& operator<<(ostream& a, const Address& b)
{
a<<"House No "<<b.house<<endl;
a<<"Block Number "<<b.block<<endl;
a<<"Town "<<b.town<<endl;
a<<"City "<<b.city<<endl;
return a;
};

Can anybody plrase help me to identify the error here? It is not
letting me access the data members of the class.

Show a complete compilable example. I don't see a problem.

--
John Carson
May 29 '06 #2

P: n/a
"John Carson" <jc****************@netspace.net.au> wrote in message
news:44***********************@un-2park-reader-01.sydney.pipenetworks.com.au

Show a complete compilable example. I don't see a problem.


In this case, I should have said: a complete example that you think should
compile but doesn't.

--
John Carson
May 29 '06 #3

P: n/a
om***********@inbox.com wrote:
#ifndef ADDRESS_H
#define ADDRESS_H

class Address
{
private:
int house;
int block;
char* town;
char* city;
public:
Address(char*,int, char*, int);
friend ostream& operator<<(ostream&, const Address&);
'ostream' is not defined.
};
#endif

ostream& operator<<(ostream& a, const Address& b)
{
a<<"House No "<<b.house<<endl;
a<<"Block Number "<<b.block<<endl;
a<<"Town "<<b.town<<endl;
a<<"City "<<b.city<<endl;
return a;
};

Can anybody plrase help me to identify the error here?
What error?
It is not letting me access the data members of the class.


What's happening instead?

May 29 '06 #4

P: n/a
When i compile the file separately it works fine but when i compile it
along with a group of files it this happens:

address.h:19: ISO C++ forbids declaration of `ostream' with no type
address.h:19: `ostream' is neither function nor member function; cannot
be
declared friend
address.h:19: parse error before `&' token
Rolf Magnus wrote:
om***********@inbox.com wrote:
#ifndef ADDRESS_H
#define ADDRESS_H

class Address
{
private:
int house;
int block;
char* town;
char* city;
public:
Address(char*,int, char*, int);
friend ostream& operator<<(ostream&, const Address&);


'ostream' is not defined.
};
#endif

ostream& operator<<(ostream& a, const Address& b)
{
a<<"House No "<<b.house<<endl;
a<<"Block Number "<<b.block<<endl;
a<<"Town "<<b.town<<endl;
a<<"City "<<b.city<<endl;
return a;
};

Can anybody plrase help me to identify the error here?


What error?
It is not letting me access the data members of the class.


What's happening instead?


May 31 '06 #5

P: n/a

<om***********@inbox.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
When i compile the file separately it works fine but when i compile it
along with a group of files it this happens:

address.h:19: ISO C++ forbids declaration of `ostream' with no type
address.h:19: `ostream' is neither function nor member function; cannot
be
declared friend
address.h:19: parse error before `&' token
Sounds like you forgot to include the header for ostream.
#include <fstream>


Rolf Magnus wrote:
om***********@inbox.com wrote:
> #ifndef ADDRESS_H
> #define ADDRESS_H
>
> class Address
> {
> private:
> int house;
> int block;
> char* town;
> char* city;
> public:
> Address(char*,int, char*, int);
> friend ostream& operator<<(ostream&, const Address&);


'ostream' is not defined.
> };
> #endif
>
> ostream& operator<<(ostream& a, const Address& b)
> {
> a<<"House No "<<b.house<<endl;
> a<<"Block Number "<<b.block<<endl;
> a<<"Town "<<b.town<<endl;
> a<<"City "<<b.city<<endl;
> return a;
> };
>
> Can anybody plrase help me to identify the error here?


What error?
> It is not letting me access the data members of the class.


What's happening instead?

May 31 '06 #6

P: n/a
Jim Langston wrote:

<om***********@inbox.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
When i compile the file separately it works fine but when i compile it
along with a group of files it this happens:

address.h:19: ISO C++ forbids declaration of `ostream' with no type
address.h:19: `ostream' is neither function nor member function; cannot
be
declared friend
address.h:19: parse error before `&' token


Sounds like you forgot to include the header for ostream.
#include <fstream>


That would be the header for (surprise!) fstream. The header for ostream
would be (surprise again!):

#include <ostream>

May 31 '06 #7

P: n/a
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:e5*************@news.t-online.com...
Jim Langston wrote:

<om***********@inbox.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
When i compile the file separately it works fine but when i compile it
along with a group of files it this happens:

address.h:19: ISO C++ forbids declaration of `ostream' with no type
address.h:19: `ostream' is neither function nor member function; cannot
be
declared friend
address.h:19: parse error before `&' token


Sounds like you forgot to include the header for ostream.
#include <fstream>


That would be the header for (surprise!) fstream. The header for ostream
would be (surprise again!):

#include <ostream>


I stand corrected. I didn't look closely enough. I didn't remember what it
was so I just opened up a program that used it and saw <fstream> and thought
that was it since I don't have #include <ostream>. I must have that in a
header file somewhere. My bad.
May 31 '06 #8

P: n/a
Jim Langston wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:e5*************@news.t-online.com...
Jim Langston wrote:
Sounds like you forgot to include the header for ostream.
#include <fstream>


That would be the header for (surprise!) fstream. The header for ostream
would be (surprise again!):

#include <ostream>


I stand corrected. I didn't look closely enough. I didn't remember what it
was so I just opened up a program that used it and saw <fstream> and thought
that was it since I don't have #include <ostream>. I must have that in a
header file somewhere. My bad.


Standard library implementation headers sometimes include other
standard headers in addition to those they are required to. This means
you get the include "for free" without being explicit about it, though
in this case "free" is a bad thing because you're relying on a
side-effect (which may change) rather than explicitly stating what you
want. Though for all I know, <fstream> always includes <ostream> --
not that that means you should use <fstream> instead, mind you.

Also, note that std::cout isn't defined in <ostream>; it's in
<iostream>.

Also, check out <iosfwd> sometime -- it doesn't pertain here, but it's
good to know about and kind of an interesting anomaly.

Luke

May 31 '06 #9

P: n/a

Luke Meyers wrote:
Also, check out <iosfwd> sometime -- it doesn't pertain here, but it's
good to know about and kind of an interesting anomaly.

Luke


It does pertain here if you ensure that <ostream> is complete where you
implement the function (Address.cpp?)

May 31 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.