C ++创建类似容器的类

时间:2013-12-17 17:25:54

标签: c++ templates containers

我正在尝试创建一个应该定义图搜索算法行为的类。

该类接收一个通用容器作为模板参数,并根据容器

执行操作
template <typename N, typename E, class Container>
class Frontier {
    private:
        Container frontier;

    public:
        bool isEmpty() { return this.frontier.empty(); }

        typename Graph<N, E>::const_iterator pop() { return this.frontier.pop(); }

        bool push(typename Graph<N, E>::const_iterator it) { return this.frontier.push(it); }
};

但是当我尝试编译时我得到了

request for member ‘frontier’ in ‘this’, which is of non-class type 

我知道这可以做到,因为stl容器是这样实现的

template<class T, Class C = deque<T> > class std::stack;

我注意到Class中的大写C所以我尝试在实现中使用Class但是我从编译器中得到了“Class not defined”。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:6)

您错过了错误消息的结尾,该消息会告诉您this是指针,并询问您是否打算使用->而不是.。这应该可以解决错误。

请注意,classtypename在模板参数列表中是等效的。没什么大不了的,但是一致性很好。 (请注意,Class无效作为关键字。不确定从哪里获得...)

template <typename N, typename E, typename Container>
class Frontier {
    private:
        Container frontier;

    public:
        bool isEmpty() { return this->frontier.empty(); }

        typename Graph<N, E>::const_iterator pop() { return this->frontier.pop(); }

        bool push(typename Graph<N, E>::const_iterator it) { return this->frontier.push(it); }
};