473,386 Members | 1,997 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.

Operator Overloading

#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
9 3523
<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
"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
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
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

<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
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
"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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.