By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

problems with Overloading operators

P: n/a
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



Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Fri, 11 Feb 2005 00:34:09 -0500, "Yudan YI \(OSU\)" <yi***@osu.edu>
wrote:
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
{

Jul 23 '05 #2

P: n/a
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" <bk***@ncf.ca> wrote in message
news:3h********************************@4ax.com...
On Fri, 11 Feb 2005 00:34:09 -0500, "Yudan YI \(OSU\)" <yi***@osu.edu>
wrote:
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
{


Jul 23 '05 #3

P: n/a
Thanks, you method works
Yudan
"Raymond Martineau" <bk***@ncf.ca> wrote in message
news:3h********************************@4ax.com...
On Fri, 11 Feb 2005 00:34:09 -0500, "Yudan YI \(OSU\)" <yi***@osu.edu>
wrote:
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
{


Jul 23 '05 #4

P: n/a
Yudan YI (OSU) wrote:
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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.