C++中派生类模板覆盖虚函数的问题

新手上路,请多包涵
// 定义基类模板
template <typename T>
class Base {
   public:
    // 声明虚函数 foo
    virtual Base* foo(const T&) = 0;
};

// 定义派生类模板
template <typename T>
class C : Base<T> {
   public:
    Base* foo(const T&) override;
};

// 在类定义域外定义foo
// error
template <typename T>
Base<T>* C<T>::foo(const T& t) {
    // function body
    return this;
}

上面这段代码在foo位置报错。
出错信息:declaration is incompatible with "<error-type> *C<T>::foo(const T &)" (declared at line 16)

具体问题和原因是什么?
正确的实现方法是什么?

阅读 2.1k
1 个回答

派生类声明虚函数的时候返回值是Base<T>*不是Base*吧

推荐问题