 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
solartimba Guest
|
Posted: Wed Oct 29, 2003 7:00 pm Post subject: overloading op of class within a class |
|
|
I wrote the following two classes with the intention of putting the
first into the second, then puting a group of the second class in a
vector to be sorted using the sort() algorithm. The problem is with
the overloaded op in the second class. Why doesn't this work given
that I already overloaded the op for the first class?
//******************************************
class cIndicatorValue
{
private:
float indVal;
float pchng_1wkLater;
float pchng_4wkLater;
float pchng_12wkLater;
public:
cIndicatorValue(): indVal(0), pchng_1wkLater(-2),
pchng_4wkLater(-2), pchng_12wkLater(-2)
{ }
void setData(float tmpIndVal, float tmp1wk, float tmp4wk, float
tmp12wk)
{
indVal=tmpIndVal;
pchng_1wkLater=tmp1wk;
pchng_1wkLater=tmp1wk;
pchng_1wkLater=tmp1wk;
}
friend bool operator<(const cIndicatorValue&, const
cIndicatorValue&);
friend bool operator==(const cIndicatorValue&, const
cIndicatorValue&);
}; //end of cIndicatorValue
bool operator<(const cIndicatorValue& p1, const cIndicatorValue& p2)
{
return(p1.indVal > p2.indVal) ? true : false;
}
bool operator==(const cIndicatorValue& p1, const cIndicatorValue& p2)
{
return(p1.indVal == p2.indVal) ? true : false;
}
//********************************************************************
class cRank
{
private:
string name;
cIndicatorValue indVal;
public:
cRank(): name("NA")
{ }
cRank(string tmpName, cIndicatorValue tmpIndVal): name(tmpName),
indVal(tmpIndVal)
{ }
void getInfo(string tmpName, cIndicatorValue tmpIndVal)
{
name=tmpName;
indVal=tmpIndVal;
}
string returnName()
{ return name; }
cIndicatorValue returnIndVal()
{ return indVal; }
friend bool operator<(const cRank&, const cRank&);
friend bool operator==(const cRank&, const cRank&);
}; //end of cRank
bool operator<(const cRank& p1, const cRank& p2)
{
return(p1.indVal > p2.indVal) ? true : false;
}
bool operator==(const cRank& p1, const cRank& p2)
{
return(p1.indVal == p2.indVal) ? true : false;
}
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Oct 29, 2003 9:17 pm Post subject: Re: overloading op of class within a class |
|
|
"solartimba" <kaferro (AT) hotmail (DOT) com> wrote
| Quote: | I wrote the following two classes with the intention of putting the
first into the second, then puting a group of the second class in a
vector to be sorted using the sort() algorithm. The problem is with
the overloaded op in the second class. Why doesn't this work given
that I already overloaded the op for the first class?
//******************************************
class cIndicatorValue
{
[...]
friend bool operator<(const cIndicatorValue&, const
cIndicatorValue&);
[...]
}; //end of cIndicatorValue
[...]
*******************************************************************
class cRank
{ [...]
};
bool operator<(const cRank& p1, const cRank& p2)
{
return(p1.indVal > p2.indVal) ? true : false;
|
First of all, if you overload operator < and then use operator >,
how do you expect the compiler to understand you?
Second, there is no sence to use ?: here. Just return the Boolean
expression:
return p1.indVal < p2.indVal;
| Quote: | }
bool operator==(const cRank& p1, const cRank& p2)
{
return(p1.indVal == p2.indVal) ? true : false;
|
Same here, just return the expression. No "true:false" necessary.
Victor
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|