扩展模板抽象类时出错
Error when extending a template abstract class
我有一个基于节点的队列实现的代码,我应该扩展一个名为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;
};
我一直得到一个expected class name before '{' token
错误,即使我包括了QueueInterface头文件。为什么会发生这种情况?
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;
};
相关文章:
- 重载操作程序时出错>>用于类中的字符串 memebr
- 无法创建抽象类的实例
- 用pybind11包装C++抽象类时出错
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 有没有办法按值将纯抽象类的所有子类传递给 C++ 中的函数?
- 抽象类错误,请参阅声明" "是抽象的
- 将自定义函数传递到基抽象类中以延迟执行
- 将包含抽象类和普通类C++包导出到 Python
- C++:处理抽象类中的错误时出现问题
- C ++如何在原始抽象类中创建一个函数,该函数接受派生类的输入
- 在 C++ 中使用另一个头文件中的抽象类
- ATL::CComContainedObject<contained>: C2259 无法实例化抽象类
- C++:从抽象类重写纯虚拟运算符重载
- 如何创建shared_ptr抽象类的容器
- .h 或.cpp文件中的抽象类或两者兼而有之?
- 从抽象类继承以创建另一个抽象类时,我应该重新声明所有虚函数吗?
- 尝试定义派生自抽象类的类类型的对象时出错
- 扩展模板抽象类时出错
- 抽象类在创建派生类的对象时出错
- 转换基指针时出错:“不能实例化抽象类”