Overloading Operator Issues
Hey All, long time reader, first time poster...
I've been working on a program that has an object (information on a circle) stored in nodes in an IndexedLinkedList. I'm having a problem getting the circle info from the list though. Here are the code snippets involved.
main function
int main(int argc, char *argv[])
{
IndexedLinkList * circleList = new IndexedLinkList();
Circle * c1 = new Circle(100, 100, RADIUS, BALLCOLOR);
Circle * c2 = new Circle(700, 100, RADIUS, BALLCOLOR);
circleList->insertFront(c1);
circleList->insertFront(c2);
while (1)
{
// Render stuff
render(circleList);
}
//some more stuff here
}
render function
void render(IndexedLinkList * list)
{
Circle * c;
for (int i = 0; i < list->getSize(); i++)
{
*c = list[i]; //error is here
drawcircle(c);
}
}
operator[] method of the indexedLinkedList Class
(head points to the node at the head of the list, current is a pointer to the current node selected.
Circle& IndexedLinkList::operator[](int index)
{
if((index >= 0) && (index < this->size))
{
this->current = this->head;
for(int i = 0; i < index; i++)
this->current = this->current->next;
return this->current->data;
}
else if(index == this->size)
{
//this->insertAt(this->size, 0);
this->insertBack(0);
return this->current->data;
}
else
throw;
}
operator= function of the Circle class
Circle& Circle::operator=(Circle * c) {
this->xPos = c->xPos;
this->yPos = c->yPos;
this->radius = c->radius;
this->color = c->color;
this->xSpeed = c->xSpeed;
this->ySpeed = c->ySpeed;
return *this;
}
I get an error saying:
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class IndexedLinkList' (or there is no acceptable conversion)
when I try to compile main.cpp
Any advice/tips/solutions would be great.
Thanks
[2318 byte] By [
Paul99] at [2007-11-11 6:33:09]

# 2 Re: Overloading Operator Issues
thanks for the tip, but it still didn't solve the problem. The same error occurs.
I've tried to set the values via a function
void Circle::setValues(Circle c) {
this->xPos = c.xPos;
this->yPos = c.yPos;
this->radius = c.radius;
this->color = c.color;
this->xSpeed = c.xSpeed;
this->ySpeed = c.ySpeed;
}
but the same error occurs, which leads me to belive that its an issue with the operator[] function of the IndexedLinkedList class.
Circle& IndexedLinkList::operator[](int index)
{
if((index >= 0) && (index < this->size))
{
this->current = this->head;
for(int i = 0; i < index; i++)
this->current = this->current->next;
return this->current->data;
}
else if(index == this->size)
{
//this->insertAt(this->size, 0);
this->insertBack(0);
return this->current->data;
}
else
throw;
}
thanks for the tip on the operator= though.
Paul99 at 2007-11-11 21:04:02 >

# 5 Re: Overloading Operator Issues
You know, I really don't see the point in this thread. You can expect us to find bugs in code that you posted, but do you really expect us to use a crystal ball to find bugs of which you didn't report in code snippets that you didn't include? Yes, you may have many other bugs elsewhere but the two previous code listinsg HAD bugs as explained in my previous posts.
If you're really looking for help, please mention ALL the relevant bugs in your post or at least don't expect something supernatural from us..
Seconldy I strongly recommend that you get a good C++ turorial and learn the basic principles of overloading operator [], how the copy constructor and the assignment operator should look like etc. At the end of the day, you need to know this stuff, no matter how useful newsgroups can be.
Finally, the code listing in your last post has several stylistic problems and bugs that are too important to be ignored:
//...
1) Circle * c = new Circle();
Why not use:
Circle c;
2) for (int i = 0; i < list->getSize(); i++) {
Normally, you pass a container by reference so there's no need to use the cumbersome -> pointer notation.
3) c->setValues((*list)[i]);
This is wierd. Linked lists by design don't define the overloaded subscript operator because a list isn't a contiguous chunk of memory. It looks like you really need to use a vector here or a queue. Secondly, this monstrosity: ((*list)[i]) isn't exactly readable and if you defined list as a reference, you would be able to get rid of the ugly casts and the pointer dereferencing.
drawcircle(c);
When exactly is circle deleted in your program?
Danny at 2007-11-11 21:07:05 >
