 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Lilith Guest
|
Posted: Sun May 20, 2007 6:10 am Post subject: Problem overloading operator= in a class |
|
|
I have a class called Intersection which contains the following with
public access...
Intersection operator= (Intersection &i);
It's defined as...
Intersection Intersection::operator= (Intersection &i)
{
this->ID = i.ID;
this->x = i.x;
[snip]
this->se = i.se;
this->ss = i.ss;
this->sw = i.sw;
}
A private method used to clear an array has the following code...
void Intersection::clearLandscape(void)
{
Intersection temp;
for (int j=0; j < 250; j++) {
Landscape[j] = temp;
}
}
Landscape is a static array member of Intersection objects and temp is
an Intersetion object that takes the default constructor.
When I compile I get the following error...
binary '=' : no operator defined which takes a right-hand operand of
type 'class Intersection' (or there is no acceptable conversion)
with regards to the Landscape[j] = temp statement.
Can anyone see where my error is?
--
TIA,
Lilith |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Sun May 20, 2007 7:43 am Post subject: Re: Problem overloading operator= in a class |
|
|
In article <f87v435hhllqpll2rhs3nar23lt0uvv3ru (AT) 4ax (DOT) com>,
Lilith <lilith (AT) dcccd (DOT) edu> wrote:
| Quote: | I have a class called Intersection which contains the following with
public access...
Intersection operator= (Intersection &i);
It's defined as...
Intersection Intersection::operator= (Intersection &i)
{
this->ID = i.ID;
this->x = i.x;
[snip]
this->se = i.se;
this->ss = i.ss;
this->sw = i.sw;
}
|
If all your op= does is assign each member to its corresponding member,
then remove the op= from your class and everything will work.
BTW, the canonical op= is "Type& operator=( const Type& );" and
sometimes you will see, "Type& operator=( Type );" or even "const Type&
operator=( const Type& );"
Your signature is rather non-standard.
| Quote: | Can anyone see where my error is?
|
Your error is that you are not showing the code that fails to compile.
Note that the code below compiles fine.
class Intersection
{
public:
Intersection operator=( Intersection& i );
};
Intersection Intersection::operator=( Intersection& i )
{
return *this;
}
void clearLandscape()
{
Intersection temp;
Intersection landscape[250];
for (int j=0; j < 250; j++) {
landscape[j] = temp;
}
} |
|
| Back to top |
|
 |
zeppe Guest
|
Posted: Sun May 20, 2007 9:11 am Post subject: Re: Problem overloading operator= in a class |
|
|
Daniel T. wrote:
| Quote: |
If all your op= does is assign each member to its corresponding member,
then remove the op= from your class and everything will work.
BTW, the canonical op= is "Type& operator=( const Type& );" and
sometimes you will see, "Type& operator=( Type );" or even "const Type&
operator=( const Type& );"
Your signature is rather non-standard.
|
It is standard. The only prolem is that is useless. There are some
situations (quite rare, to be fair, given the usual semantic of the
operator=) in qhich you may want to change something in the right
operand of the '='.
Regards,
Zeppe |
|
| 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
|
|