我正在尝试创建一个应该定义图搜索算法行为的类。
该类接收一个通用容器作为模板参数,并根据容器
执行操作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”。我该如何解决这个问题?
答案 0 :(得分:6)
您错过了错误消息的结尾,该消息会告诉您this
是指针,并询问您是否打算使用->
而不是.
。这应该可以解决错误。
请注意,class
和typename
在模板参数列表中是等效的。没什么大不了的,但是一致性很好。 (请注意,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); }
};