Overloading Operator Issues


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Overloading Operator Issues

  1. #1
    Join Date
    Aug 2005
    Posts
    3

    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
    Code:
    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
    Code:
    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.
    Code:
    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
    Code:
    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

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    Welcome aboard;)

    operator= is supposed to take a reference as its argumen, not a pointer. Usually the argument is const qualified:

    Code:
    Circle& Circle::operator=(const Circle & c)
    {
    //..
    }
    Last edited by Danny; 08-03-2005 at 11:47 PM.
    Danny Kalev

  3. #3
    Join Date
    Aug 2005
    Posts
    3
    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
    Code:
    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.

    Code:
    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.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    You didn't hsow how you defined data but I guess you're right: it should be of type Circle but it isn't so retruning another type is an error. But still, your setValues isn't right. You need to pass the argument as reference to const:

    void setValues(const Circle & c);

    BTW, when overloading operator [], you need two versions: a const one and a non-const one. You only have the latter.
    Last edited by Danny; 08-04-2005 at 02:55 AM.
    Danny Kalev

  5. #5
    Join Date
    Aug 2005
    Posts
    3
    Thanks for the help, but I found the error was in not dereferencing the indexed linked list pointer that was passed to the render method.

    in the render class.
    Code:
    //....
    
      Circle * c = new Circle();
      for (int i = 0; i < list->getSize(); i++) {
    
    	  c->setValues((*list)[i]);
    
    	  drawcircle(c);
      }
    //...

    Thanks for the help though.


    Paul

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    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)
    Code:
    Circle * c = new Circle();
    Why not use:
    Code:
    Circle c;
    2)
    Code:
     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)
    Code:
     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:
    Code:
    ((*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.

    Code:
    	  drawcircle(c);
    When exactly is circle deleted in your program?
    Last edited by Danny; 08-07-2005 at 02:44 AM.
    Danny Kalev

Similar Threads

  1. Operator Overloading & Inheritance
    By joship1980 in forum C++
    Replies: 6
    Last Post: 06-02-2005, 12:18 AM
  2. Operator Overloading
    By sivsu in forum C++
    Replies: 1
    Last Post: 03-14-2005, 02:28 PM
  3. Replies: 18
    Last Post: 11-09-2001, 01:07 PM
  4. Re: Operator Overloading.....C# can do it....
    By Jonathan Allen in forum .NET
    Replies: 5
    Last Post: 11-09-2001, 01:04 PM
  5. Operator Overloading
    By Jeff Pipes in forum .NET
    Replies: 1
    Last Post: 06-28-2001, 12:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center