Operator Overloading

O

omariqbalnaru

#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.
 
J

John Carson

#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.
 
J

John Carson

John Carson said:
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.
 
R

Rolf Magnus

#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?
 
O

omariqbalnaru

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
 
J

Jim Langston

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.
 
R

Rolf Magnus

Jim said:
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>
 
J

Jim Langston

Rolf Magnus said:
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.
 
L

Luke Meyers

Jim said:
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
 
E

Earl Purple

Luke said:
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?)
 

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

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top