Connecting Tech Pros Worldwide Forums | Help | Site Map

problems with Overloading operators

Yudan YI \(OSU\)
Guest
 
Posts: n/a
#1: Jul 23 '05
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::operator- (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








Raymond Martineau
Guest
 
Posts: n/a
#2: Jul 23 '05

re: problems with Overloading operators


On Fri, 11 Feb 2005 00:34:09 -0500, "Yudan YI \(OSU\)" <yi.58@osu.edu>
wrote:
[color=blue]
>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[/color]

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

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

You need to put a forward declaration. For example:

class TObsSD;

class TObsData
{





Yudan YI \(OSU\)
Guest
 
Posts: n/a
#3: Jul 23 '05

re: problems with Overloading operators


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

"Raymond Martineau" <bk039@ncf.ca> wrote in message
news:3hho01pc4t0c868a1s5psn5jh57blihcmd@4ax.com...[color=blue]
> On Fri, 11 Feb 2005 00:34:09 -0500, "Yudan YI \(OSU\)" <yi.58@osu.edu>
> wrote:
>[color=green]
>>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[/color]
>
> This is one reason to avoid "//" comments while posting on usenet. Some
> news-readers will perform word-wrap and introduce unexpected errors.
>[color=green]
>>// object of 2nd class defined later[/color]
>
> The error occurrs because class TObsSD isn't yet defined.
>
> You need to put a forward declaration. For example:
>
> class TObsSD;
>
> class TObsData
> {
>
>
>
>
>[/color]


Yudan YI \(OSU\)
Guest
 
Posts: n/a
#4: Jul 23 '05

re: problems with Overloading operators


Thanks, you method works
Yudan
"Raymond Martineau" <bk039@ncf.ca> wrote in message
news:3hho01pc4t0c868a1s5psn5jh57blihcmd@4ax.com...[color=blue]
> On Fri, 11 Feb 2005 00:34:09 -0500, "Yudan YI \(OSU\)" <yi.58@osu.edu>
> wrote:
>[color=green]
>>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[/color]
>
> This is one reason to avoid "//" comments while posting on usenet. Some
> news-readers will perform word-wrap and introduce unexpected errors.
>[color=green]
>>// object of 2nd class defined later[/color]
>
> The error occurrs because class TObsSD isn't yet defined.
>
> You need to put a forward declaration. For example:
>
> class TObsSD;
>
> class TObsData
> {
>
>
>
>
>[/color]


Ole Reinartz
Guest
 
Posts: n/a
#5: Jul 23 '05

re: problems with Overloading operators


Yudan YI (OSU) wrote:[color=blue]
> 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[/color]

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
Closed Thread