473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
May 20 '07 #1
6 1308
In article <f8********************************@4ax.com>,
Lilith <li****@dcccd.eduwrote:
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.
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;
}
}
May 20 '07 #2
Daniel T. wrote:
>
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
May 20 '07 #3
Lilith wrote:
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::operator= (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?

May 20 '07 #4
On Sun, 20 May 2007 11:24:52 +0200, Rolf Magnus <ra******@t-online.de>
wrote:
>Lilith wrote:
>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::operator= (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.

--
Lilith
May 20 '07 #5
On Sun, 20 May 2007 02:43:57 GMT, "Daniel T." <da******@earthlink.net>
wrote:
>In article <f8********************************@4ax.com>,
Lilith <li****@dcccd.eduwrote:
>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.
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.
>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.
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::operator=( 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.

--
Lilith
May 20 '07 #6
On May 20, 4:40 am, zeppe <z...@nospam.remove.all.this.email.it>
wrote:
Daniel T. wrote:
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 '='.
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.

May 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.