Hey Folks, I need some help again. We have an assignment that is incorporating both nested classes and operator overloading. The problem I'm running into is the fact that I don't understand how to implement the methods. Here's an example:
Obviously we'll have to implement both a begin() and an end() method that both return an "Iterator" object (along with the others but right now I'm stuck on these two). This is what I currently have:
I've spoken with our professor; he did say my constructor is correct and he did steer me in the direction of the returnnew... code for the begin() method, which I actually already had based on documentation and previous assignments, but I just can't figure out what I'm missing to get this thing done. I know this is relatively simple/trivial stuff but it's driving me crazy. Again, I'm not looking for someone to do my work for me, just to explain or help me understand the concept(s) I'm missing.
Are you sure he didn't steer you in that direction because he think you should not use new? Look at the end() function, you are not using new there, so why do you use it in begin()?
> he did steer me in the direction of the return new... code for the begin() method
weird, because that's wrong. new Iterator(*this) would not return an iterator, but a pointer to an iterator.
Also, that constructor does not exist, you are missing the `cursor' parameter.
> but I just can't figure out what I'm missing to get this thing done
If you don't say what you are supposed to do it's hard to help.
By the way, the ++ operator should return an iterator.
I remember that when I made a nested iterator class for a doubly linked list, all it had for a private data member was a pointer to a node. The begin and end functions:
The ++ and -- operator just called node = node->next and node = node->prev. Don't know if that helps, since I have no idea if your STLDataBuffer class is like a linked list at all.
@Peter87 - It wouldn't surprise me; throughout this entire semester, he's given us bad information and he frequently mixes things up. The end() method isn't correct either, that was just my first stab at getting something in there; I'm just trying to get begin() to work before tackling the other methods.
@ne555 - See what I just wrote to Peter87; it wouldn't surprise me if that's wrong. Regarding the missing cursor, I know what you're saying but when I try to add that additional parameter, it still doesn't compile. Without the cursor parameter, I get:
no matching function for call to 'STLDataBuffer::Iterator::Iterator(STLDataBuffer&)'
The compiler (GCC with -std=c++0x) tells me a candidate is 'STLDataBuffer::Iterator::Iterator(STLDataBuffer*, int)' but if I add an int (I.E. 0 or 'cursor') to that 'return new...' statement, I get:
no matching function for call to 'STLDataBuffer::Iterator::Iterator(STLDataBuffer&, int)'
We're just dealing with a simple integer array and the assignment is simply to iterate through the array and 'cout' the elements. This is the main() he gave us to use:
FYI, the 'DataBuffer' class is from a previous assignment and is unchanged for this assignment, 'STLDataBuffer' is supposed to just be a sub-class of 'DataBuffer'.