Nested Class Question

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:

Header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#ifndef STLDATABUFFER_H
#define STLDATABUFFER_H

#include "DataBuffer.h"
class STLDataBuffer : public DataBuffer
{
public:
    class Iterator
    {
    private:
        STLDataBuffer* target;
        int cursor;
        static const int null = 0;
        Iterator(STLDataBuffer* target, int cursor);
    public:
        Iterator();
        bool operator==(const Iterator& rhs) const;
        bool operator!=(const Iterator& rhs) const;
        void operator++();
        void operator++(int);
        const int& operator*();
        friend class STLDataBuffer;
    };
    Iterator begin();
    Iterator end();
};

#endif // STLDATABUFFER_H


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include "STLDataBuffer.h"

STLDataBuffer::Iterator::Iterator(STLDataBuffer* target, int cursor)
{
    this->target = target;
    this->cursor = cursor;
}

STLDataBuffer::Iterator STLDataBuffer::begin()
{
    return (new Iterator(*this));
}

STLDataBuffer::Iterator STLDataBuffer::end()
{
    return Iterator();
}

bool STLDataBuffer::Iterator::operator==(const Iterator& rhs) const
{
    return ((*this) == rhs);
}

bool STLDataBuffer::Iterator::operator!=(const Iterator& rhs) const
{
    return !((*this) == rhs);
}

void STLDataBuffer::Iterator::operator++()
{

}

void STLDataBuffer::Iterator::operator++(int)
{

}


I've spoken with our professor; he did say my constructor is correct and he did steer me in the direction of the return new... 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.

Thank you!
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class T>
typename List<T>::iterator List<T>::begin()
{
    iterator it;
    it.node = head;
    return it;
}

template <class T>
typename List<T>::iterator List<T>::end()
{
    iterator it; //default constructor sets it.node = nullptr
    return it;
}


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.

20
21
22
23
bool STLDataBuffer::Iterator::operator==(const Iterator& rhs) const
{
    return ((*this) == rhs);
}

^ This is totally wrong. That's like a dictionary entry like this:

chicken
    A chicken.
Last edited on
Wouldn't begin() return the lhs of the data buffer and end() the rhs? Is DataBuffer overloaded from an STL container or something you made?

Edit: Sorry, end() is one past the rhs
Last edited on
@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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>
#include "DataBuffer.h"
#include "STLDataBuffer.h"

using namespace std;

int main()
{
    STLDataBuffer buffer;
    const int ARR0_LEN = 3;
    int arr0[ARR0_LEN] = { 4, 7, 3 };
    buffer.copyFromArray(arr0, ARR0_LEN);
    for (STLDataBuffer::Iterator iter = buffer.begin(); iter != buffer.end();
            iter++)
        cout << *iter << " ";
    cout << endl;
    int arr[] = { 3, 6, 8, 9, 1, 5, 3, 2, 7, 12, 4, 8 };
    buffer.copyFromArray(arr, 12);
    for (STLDataBuffer::Iterator iter = buffer.begin(); iter != buffer.end();
            iter++)
        cout << *iter << " ";
    cout << endl;
    return 0;
}


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'.
The constructor ask for a pointer and an integer.
1
2
3
4
STLDataBuffer::Iterator STLDataBuffer::begin()
{
    return Iterator(this, 0);
}



> the 'DataBuffer' class is from a previous assignment and is unchanged for this assignment
we are not in your class.
Topic archived. No new replies allowed.