If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

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.

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.