2

So as the title suggests, I've made my own template for an iterator which looks like this:

template <class Element>
class IteratorDL {
private:
    DoubleNode<Element> *current;
public:
    IteratorDL(DoublyList<Element>);
    ~IteratorDL();
    void next();
    void back();
    bool isValid();
    Element getCurrent();
};

template <class Element>
IteratorDL<Element>::IteratorDL(DoublyList<Element> dl) {
    current = dl.first;
    cout << current->info; //this will return to me the correct value that I'm looking for
}

template <class Element>
Element IteratorDL<Element>::getCurrent() {
    if (isValid()) {
        COUT << current->info; // this will return to me something like an address, random numbers, I don't know.
        return current->info;
    }
    else {
        return 0;
    }
}

I've only posted the 2 functions that really trouble me. I'm supposed to use this over a Doubly Linked list (same, custom made). DoubleNode looks like this: template

class DoubleNode {
private:
    Element info;
    DoubleNode *next;
    DoubleNode *back;

The problem I'm having is that getCurrent() doesn't return what it's supposed to return to me the value contained in the node current but it returns to me something like an address, I can't really tell. I commented the iterator code so you can understand better what happens and I'm pretty new to c++, that's why maybe I don't understand where things go wrong.

I also tried making the linked list as a private part of the iterator but it still didn't solve the problem. After further research, I believe getCurrent() is returning some other part of memory so for some reason, in that certain function, current->info doesn't lead to where it's supposed to?

I tried this approach as well:

private:
    DoublyList<Element> dl;
    DoubleNode<Element> *current;

But same problem

Edit with code calling the functions:

DoublyList<int> dl;
    dl.addFirst(1);
    dl.addFirst(2);
    dl.addFirst(3);
    dl.addFirst(4);
    int c;
    IteratorDL<int> *it = new IteratorDL<int>(dl);
    cout << it->getCurrent(); //this should return 4 but it returns random number
//  c = it->getCurrent();
//  assert(c == 4);

Edit for the doubly list declaration:

template <class Element>
class DoublyList {
private:
    DoubleNode<Element> *first;
    DoubleNode<Element> *last;
    int count;

1 Answer 1

1

In constructor code there is current = dl.first Current is a pointer and you assign it with a pointer value that you get from constructor copy parameter.
When you call getCurrent() the memory pointed by current is no more valid at getCurrent call because a temporary copy of list has been created at constructor call and that no more exists after this call.(if you didn't define copy constructor the compiler simply copy members of class)
By adding & before parameter name this become a parameter by reference meaning that code directly deal with the original list through the reference and as the list is still alive at getCurrent call it works

Sign up to request clarification or add additional context in comments.

9 Comments

I edited my comment a few moments ago thinking the same, that in getCurrent(), current->info doesn't lead to the same part of memory where I want it to. I tried making the linked list a part of the iterator, and in constructor to have something like this->dl = dll; current = dl.first; so that my list remains alive but it didn't solve the problem
Could you copy/paste the code calling iterator constructor and getCurrent call?
Done. Edited my post and added the code at the end. I tried it as pointer, as non pointer, I triedmaking the list a pointer, changed all the code and everytime, getCurrent() returned something wrong
How is declared DoublyList ?
Edited the post and added that too
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.