抽象类:成员函数“虚拟...”的抽象返回类型无效 [英] Abstract class : invalid abstract return type for member function ‘virtual...’

查看:104
本文介绍了抽象类:成员函数“虚拟...”的抽象返回类型无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my program I have such class hierarchy:

#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)
{
}

,但是我在编译过程中仍然遇到错误。我无法更改班级层次结构,但是可以在这里做些我想做的事吗?

but Im still getting errors in compilation process. I cant change my class hierarchy but it is possible to make something I want here?

编辑的班级:

#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 按值,因为 root 是抽象的,因此永远不会存在 root 。

You cannot return a root by value, since root is abstract and thus there can never exist any values of type root.

您可能想返回一个指针:

You might want to return a pointer:

#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) };
}

您的感觉很像克隆或虚拟副本函数,但是:

What you have feels a lot like a "clone" or "virtual copy" function, though:

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

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

自从您询问引用以来,这是您可以做的另一件事,尽管似乎没有意义:

Since you asked about references, here's another thing you could do, though it seems a bit pointless: Pick a reference to one among several derived objects and return a base reference.

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

这篇关于抽象类:成员函数“虚拟...”的抽象返回类型无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆