2016-09-28 169 views
0

我有一个基于节点的队列实现的代码,我应该扩展一个名为QueueInterface的抽象类。扩展模板抽象类时出错

template<typename T> 
struct QueueInterface { 
    public: 
    virtual ~QueueInterface(){}; 
    virtual bool isEmpty() const = 0; 
    virtual void enqueue(const T value) = 0; 
    virtual void dequeue() throw(PreconditionViolationException) = 0; 
    virtual T peekFront() const throw(PreconditionViolationException) = 0; 
};  

template<typename T> 
struct Queue : QueueInterface { 
    Queue(); 
    ~Queue(); 
    bool isEmpty() const; 
    void enqueue(const T value); 
    void dequeue() throw(PreconditionViolationException); 
    T peekFront() const throw(PreconditionViolationException); 

private: 
    Node<T>* front; 
    Node<T>* back; 
}; 

即使包含QueueInterface头文件,我也会收到expected class name before '{' token错误。这是为什么发生?

+1

最有可能是'Node.h'或'QueueInterface.h'中的拼写错误。 – NathanOliver

+0

没有。我检查了。另外,为什么'Node.h'中的拼写错误会导致错误 – pyro97

+0

这将是一个猜测的结果,而不知道其他文件的内容。你能尽可能地减少它们,同时还能得到这个错误吗? TIA(P.S:我的钱在失踪;在一个类声明结束时) – Borgleader

回答

1

QueueInterface不是一个类。你可以继承不是结构体或类的东西。这个东西就是所谓的模板类。您可以在模板类之前识别模板template<...>。您必须指定一个类型,以便编译器可以创建该类型的类。

就你而言,你正试图创建一个也是模板的结构。通过查看您的基类的方法覆盖,我猜你试图做到这一点:

template<typename T> 
struct Queue : QueueInterface<T> { 
    // notice that there ---^--- you are sending the required parameter 

    // defaulted members are good. 
    Queue() = default; 

    // override too. 
    bool isEmpty() const override; 
    void enqueue(const T value) override; 
    void dequeue() throw(PreconditionViolationException) override; 
    T peekFront() const throw(PreconditionViolationException) override; 

private: 
    Node<T>* front; 
    Node<T>* back; 
};