开发者

C++ iterator for custom template class

I have nested iterator in my custom stack template class. The problem I get now is that my nested iterator's constructor does not match when I create an iterator for my stack in the main. I wonder what the problem could be.

template <class T>
class stack
{
private:
    int top;
    T st[100];

public:
    class my_iterator
    {
    public:
        my_iterator() : list(0), curr(0) {};

    private:

        stack<T> list;
        int curr;
    };

//publics in stack class
public:
    stack();
    void push(T i);
    T pop();
    void print();
    bool is_full();
    my_iterator begin() {};

};

and I create the iterator for stack with the following line in main

sta开发者_开发百科ck<double>::my_iterator it;


It looks like you forgot a *:

class my_iterator
{
public:
    my_iterator() : list(0), curr(0) {};
private:
    stack<T>* pStack; // Pointer to a stack.
    int curr;
};

Of course, you need more members (including better constructors) for the class. But this should at least allow you to create default iterators that do not point to any stack in particular.


I think the problem is that your default constructor for stack class doesn't accept any parameters. But here:

my_iterator() : list(0), curr(0) {};

you pass 0 to ctor.


The problem is the expression list(0).

Instead change to

my_iterator() : curr(0) {};

The list member will be default initialized. If you want any specific construction to be done refer the list constructors

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜