473,770 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator Overloading

#ifndef ADDRESS_H
#define ADDRESS_H

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

ostream& operator<<(ostr eam& a, const Address& b)
{
a<<"House No "<<b.house<<end l;
a<<"Block Number "<<b.block<<end l;
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 3563
<om***********@ inbox.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com
#ifndef ADDRESS_H
#define ADDRESS_H

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

ostream& operator<<(ostr eam& a, const Address& b)
{
a<<"House No "<<b.house<<end l;
a<<"Block Number "<<b.block<<end l;
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.n et.au> wrote in message
news:44******** *************** @un-2park-reader-01.sydney.pipen etworks.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***********@i nbox.com wrote:
#ifndef ADDRESS_H
#define ADDRESS_H

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

ostream& operator<<(ostr eam& a, const Address& b)
{
a<<"House No "<<b.house<<end l;
a<<"Block Number "<<b.block<<end l;
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***********@i nbox.com wrote:
#ifndef ADDRESS_H
#define ADDRESS_H

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


'ostream' is not defined.
};
#endif

ostream& operator<<(ostr eam& a, const Address& b)
{
a<<"House No "<<b.house<<end l;
a<<"Block Number "<<b.block<<end l;
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.goo glegroups.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***********@i nbox.com wrote:
> #ifndef ADDRESS_H
> #define ADDRESS_H
>
> class Address
> {
> private:
> int house;
> int block;
> char* town;
> char* city;
> public:
> Address(char*,i nt, char*, int);
> friend ostream& operator<<(ostr eam&, const Address&);


'ostream' is not defined.
> };
> #endif
>
> ostream& operator<<(ostr eam& a, const Address& b)
> {
> a<<"House No "<<b.house<<end l;
> a<<"Block Number "<<b.block<<end l;
> 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.goo glegroups.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.goo glegroups.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
2619
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, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
34
6470
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 operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
16
3096
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 has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
2
2067
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 overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
67
8669
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 in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
3
2301
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
3629
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: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
3
3282
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, obj2 ;
9
3513
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 allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow us to get rid of all the temporary arrays produced by NumPy. For example, consider the...
8
2976
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, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.