c++ - 抽象类 : invalid abstract return type for member function ‘virtual...’

标签 c++ virtual abstract-class

在我的程序中我有这样的类层次结构:

#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;

class aa;
class bb;

class root
{
public:
    virtual ~root() {}
    virtual root add(const aa& a) const=0;
    virtual root add(const bb& a) const=0;
};

class aa: public root
{
public:
    aa() { }
    aa(const aa& a) { }

    virtual root add(const aa& a) const
    { return root(new aa()); }
    virtual root add(const bb& a) const
    { return root(new bb()); }
};

class bb: public root
{
public:
    bb() { }
    bb(const bb& b) {}

    virtual root add(const aa& a) const
    { return root(new bb()); }
    virtual root add(const bb& a) const
    { return root(new bb()); }
};

int main(int argc, char **argv)
{
}

但我在编译过程中仍然遇到错误。我不能更改我的类层次结构,但可以在这里制作我想要的东西吗?

编辑类:

#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;

class root
{
public:
    virtual ~root() {}
    virtual root add(const root& a) const=0;
    virtual root add(const root& b) const=0;
};

class aa: public root
{
public:
    aa() { }
    aa(const aa& a) { }

    virtual root add(const root& a) const
    { return root(new aa()); }
    virtual root add(const root& b) const
    { return root(new bb()); }
};

class bb: public root
{
public:
    bb() { }
    bb(const bb& b) {}

    virtual root add(const root& a) const
    { return root(new bb()); }
    virtual root add(const root& b) const
    { return root(new bb()); }
};

int main(int argc, char **argv)
{
}

编辑类的错误:

/home/brian/Desktop/Temp/Untitled2.cpp|11|error: ‘virtual root root::add(const root&) const’ cannot be overloaded|
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: with ‘virtual root root::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: invalid abstract return type for member function ‘virtual root root::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   because the following virtual functions are pure within ‘root’:|
/home/brian/Desktop/Temp/Untitled2.cpp|10|note:     virtual root root::add(const root&) const|
/home/brian/Desktop/Temp/Untitled2.cpp|11|error: invalid abstract return type for member function ‘virtual root root::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|20|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|22|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|22|error: ‘virtual root aa::add(const root&) const’ cannot be overloaded|
/home/brian/Desktop/Temp/Untitled2.cpp|20|error: with ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root aa::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|20|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|21|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root aa::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|22|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected primary-expression before ‘(’ token|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected type-specifier before ‘bb’|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected ‘)’ before ‘bb’|
/home/brian/Desktop/Temp/Untitled2.cpp|32|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|34|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|34|error: ‘virtual root bb::add(const root&) const’ cannot be overloaded|
/home/brian/Desktop/Temp/Untitled2.cpp|32|error: with ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root bb::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|32|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|33|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root bb::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|34|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|35|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note:   since type ‘root’ has pure virtual functions|
||=== Build finished: 38 errors, 0 warnings ===|

最佳答案

您不能返回 root按值(value)计算,因为 root是抽象的,因此永远不会存在 root 类型的任何值.

你可能想要返回一个指针:

#include <memory>

std::unique_ptr<root> do_you_feel_lucky(aa const & x, bb const & y)
{
    if (rand() % 2 == 0)
        return { new aa(x) };
    else
        return { new bb(y) };
}

不过,您所拥有的感觉很像“克隆”或“虚拟拷贝”功能:

struct Base
{
    virtual std::unique_ptr<Base> clone() const = 0;
};

struct Derived : Base
{
    virtual std::unique_ptr<Base> clone() const
    {
        return { new Derived(*this); }
    }
};

既然你询问了引用,那么你可以做另一件事,尽管它看起来有点毫无意义:从几个派生对象中选择一个引用并返回一个基引用。

root & pick_one_from_two(aa & x, bb & y)
{
    return rand() % 2 == 0 ? x : y;
}

关于c++ - 抽象类 : invalid abstract return type for member function ‘virtual...’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16457603/

相关文章:

c++ - 除了 typedef 之外,是否有更好的方法将名称引入类范围?

c# - 为什么在 C# 主从模型中使用 virtual 关键字?

c++ - CRTP 避免动态多态性

java - 如何通过继承抽象类的类从 main 调用抽象类的静态变量访问器?

c# - 是否可以为抽象类编写扩展方法

c++ - 如何使用类型特征来定义部分抽象的模板基类?

c++ - 创建用户所需的输入对象数 C++

c++ - std::begin 是空的 std::valarray 未定义行为吗?

c++ - 使 visual studio 编译,以便最终用户不需要 c++ 运行时库

没有指针或引用的c++虚函数调用