Problem overloading operator= in a class

L

Lilith

I have a class called Intersection which contains the following with
public access...

Intersection operator= (Intersection &i);

It's defined as...

Intersection Intersection::eek:perator= (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?
 
D

Daniel T.

Lilith said:
I have a class called Intersection which contains the following with
public access...

Intersection operator= (Intersection &i);

It's defined as...

Intersection Intersection::eek:perator= (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.
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::eek:perator=( Intersection& i )
{
return *this;
}

void clearLandscape()
{
Intersection temp;
Intersection landscape[250];
for (int j=0; j < 250; j++) {
landscape[j] = temp;
}
}
 
Z

zeppe

Daniel said:
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
 
R

Rolf Magnus

Lilith said:
I have a class called Intersection which contains the following with
public access...

Intersection operator= (Intersection &i);

Are you sure you want to return by copy and take a reference to non-const
(implying that you are modifying your argument).
It's defined as...

Intersection Intersection::eek:perator= (Intersection &i)
{
this->ID = i.ID;
this->x = i.x;
[snip]
this->se = i.se;
this->ss = i.ss;
this->sw = i.sw;
}

You forgot to return something.
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?

What type is Landscape[j]? Is it really Intersection?
 
L

Lilith

Lilith said:
I have a class called Intersection which contains the following with
public access...

Intersection operator= (Intersection &i);

Are you sure you want to return by copy and take a reference to non-const
(implying that you are modifying your argument).
It's defined as...

Intersection Intersection::eek:perator= (Intersection &i)
{
this->ID = i.ID;
this->x = i.x;
[snip]
this->se = i.se;
this->ss = i.ss;
this->sw = i.sw;
}

You forgot to return something.

That I did, though it shouldn't have have any effect on the intended
result since that is all taken care of in the remaining body of the
method. Perhaps that's what the compiler is complaining about, though
it would be more helpful to say "your function does not return a
value..".
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?

What type is Landscape[j]? Is it really Intersection?

Yes. See the **** above.
 
L

Lilith

Lilith said:
I have a class called Intersection which contains the following with
public access...

Intersection operator= (Intersection &i);

It's defined as...

Intersection Intersection::eek:perator= (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.

I tried that and it still issued the same error. It's as if it thinks
I'm referencing a totally different class.
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.

I realized that later and fixed it. Still the same results though.
Your error is that you are not showing the code that fails to compile.
Note that the code below compiles fine.

Did I not? I thought it was stated in the part where I said:

"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."

I avoided putting markers on the code itself for fear of obscuring
what was original and what was a marker.

However....
class Intersection
{
public:
Intersection operator=( Intersection& i );
};

Intersection Intersection::eek:perator=( Intersection& i )
{
return *this;
}

void clearLandscape()
{
Intersection temp;
Intersection landscape[250];
for (int j=0; j < 250; j++) {
landscape[j] = temp;
}
}

Ach! That helped me locate the error. I erroneously declared
Landscape as type IStruct, a structure with similar members to
Intersection which I will be using to set values from a series of
IStruct arrays at various parts of the program. Once it compiled with
Landscape declared local to the function I started looking at the
original declaration of Landscape and discovered my error.

Many thanks for your help.
 
D

Daniel T.

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 '='.

Accepted. That is why I put that as a "standard" option in my post.
The OP however, returns a newly created object, not a reference or
const reference.
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top