problems with Overloading operators

Y

Yudan YI \(OSU\)

Hi
I have a problem with the overloading operators. My code is followed

header file
class TObsData
{
public:
string stnname_
double obs_;
~TObsData() {};
TObsData();
TObsData(const TObsData& obsDataSrc);
TObsData& operator= (const TObsData& obsDataSrc);
friend ostream& operator<<(ostream& os, const TObsData& obsDataSrc);
TObsSD operator- (const TObsData& obsDataSrc); // <= I want to
define an operator - and the result is another
// object of 2nd class defined later
...
}

class TObsSD
{
public:
TObsData obs_[2]; // the class contain the definition for the 1st
class
...
}


then i define the overload operator- for the TObsData class in the *.cpp
file

TObsSD TObsData::eek:perator- (const TObsData& obsDataSrc)
{
TObsSD obsSD;
obsSD.obs_[0] = *this;
obsSD.obs_[1] = obsDataSrc;
return (obsSD);
}


a error will shown at "TObsSD operator- (const TObsData& obsDataSrc); "
when I try to complie the project

do you guy know what cause this problem?

Thanks
Yudan
 
R

Raymond Martineau

Hi
I have a problem with the overloading operators. My code is followed

header file
class TObsData
{
public:
string stnname_
double obs_;
~TObsData() {};
TObsData();
TObsData(const TObsData& obsDataSrc);
TObsData& operator= (const TObsData& obsDataSrc);
friend ostream& operator<<(ostream& os, const TObsData& obsDataSrc);
TObsSD operator- (const TObsData& obsDataSrc); // <= I want to
define an operator - and the result is another

This is one reason to avoid "//" comments while posting on usenet. Some
news-readers will perform word-wrap and introduce unexpected errors.
// object of 2nd class defined later

The error occurrs because class TObsSD isn't yet defined.

You need to put a forward declaration. For example:

class TObsSD;

class TObsData
{
 
Y

Yudan YI \(OSU\)

But, the problem is that I want to use the 1st class in my 2nd class. if I
put the 2nd class definition, it still will have problem. Any good apprach
to fix it?
Thanks
 
Y

Yudan YI \(OSU\)

Thanks, you method works
Yudan
Raymond Martineau said:
This is one reason to avoid "//" comments while posting on usenet. Some
news-readers will perform word-wrap and introduce unexpected errors.


The error occurrs because class TObsSD isn't yet defined.

You need to put a forward declaration. For example:

class TObsSD;

class TObsData
{
 
O

Ole Reinartz

Yudan said:
But, the problem is that I want to use the 1st class in my 2nd class. if I
put the 2nd class definition, it still will have problem. Any good apprach
to fix it?
Thanks

Maybe you can make operator - to return a reference to const? Like
const TObsSD& operator- (const TObsData& obsDataSrc) const;

Then you might get away forward declaring TObsSD. Your reference will
most probably point to a temporary (it should, at least), but AFAIR it
is mentioned somewhere in the standard that the temporary must not be
destroyed while a reference exists that points to it. What was the
clause again?

A yes, and your opreator shuold be const. It shouldn't really change its
operands, right?

Ole
 

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,777
Messages
2,569,604
Members
45,217
Latest member
topweb3twitterchannels

Latest Threads

Top