抽象类:成员函数“virtual...”的抽象返回类型无效

2023-12-25

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

#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按价值计算,因为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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

抽象类:成员函数“virtual...”的抽象返回类型无效 的相关文章

  • 将复选框添加到 UniformGrid

    我正在尝试将复选框动态添加到 wpf 中的统一网格中 但看起来网格没有为它们分配足够的空间 所以它们都有点互相重叠 这就是我将它们添加到后面的代码中的方法 foreach string folder in subfolders PathCh
  • 检查两个数是否是彼此的排列?

    给定两个数字 a b 使得 1 例如 123 是 312 的有效排列 我也不想对数字中的数字进行排序 如果您指的是数字的字符 例如 1927 和 9721 则 至少 有几种方法 如果允许排序 一种方法是简单地sprintf将它们放入两个缓冲
  • 如何避免情绪低落?

    我有一个实现状态模式每个状态处理从事件队列获取的事件 根据State因此类有一个纯虚方法void handleEvent const Event 事件继承基础Event类 但每个事件都包含其可以是不同类型的数据 例如 int string
  • C++ 子字符串返回错误结果

    我有这个字符串 std string date 20121020 我正在做 std cout lt lt Date lt lt date lt lt n std cout lt lt Year lt lt date substr 0 4 l
  • 如何将图像和 POST 数据上传到 Azure 移动服务 ApiController 终结点?

    我正在尝试上传图片and POST表单数据 尽管理想情况下我希望它是json 到我的端点Azure 移动服务应用 我有ApiController method HttpPost Route api upload databaseId sea
  • 将目录压缩为单个文件的方法有哪些

    不知道怎么问 所以我会解释一下情况 我需要存储一些压缩文件 最初的想法是创建一个文件夹并存储所需数量的压缩文件 并创建一个文件来保存有关每个压缩文件的数据 但是 我不被允许创建许多文件 只能有一个 我决定创建一个压缩文件 其中包含有关进一步
  • 如果使用 SingleOrDefault() 并在数字列表中搜索不在列表中的数字,如何返回 null?

    使用查询正数列表时SingleOrDefault 当在列表中找不到数字时 如何返回 null 或像 1 这样的自定义值 而不是类型的默认值 在本例中为 0 你可以使用 var first theIntegers Cast
  • 如何将图像路径保存到Live Tile的WP8本地文件夹

    我正在更新我的 Windows Phone 应用程序以使用新的 WP8 文件存储 API 本地文件夹 而不是 WP7 API 隔离存储文件 旧的工作方法 这是我如何成功地将图像保存到 共享 ShellContent文件夹使用隔离存储文件方法
  • 在数据库中搜索时忽略空文本框

    此代码能够搜索数据并将其加载到DataGridView基于搜索表单文本框中提供的值 如果我将任何文本框留空 则不会有搜索结果 因为 SQL 查询是用 AND 组合的 如何在搜索 从 SQL 查询或 C 代码 时忽略空文本框 private
  • 如何衡量两个字符串之间的相似度? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 给定两个字符串text1 and text2 public SOMEUSABLERETURNTYPE Compare string t
  • 如何使我的表单标题栏遵循 Windows 深色主题?

    我已经下载了Windows 10更新包括黑暗主题 文件资源管理器等都是深色主题 但是当我创建自己的 C 表单应用程序时 标题栏是亮白色的 如何使我自己的桌面应用程序遵循我在 Windows 中设置的深色主题 你需要调用DwmSetWindo
  • C - 直接从键盘缓冲区读取

    这是C语言中的一个问题 如何直接读取键盘缓冲区中的数据 我想直接访问数据并将其存储在变量中 变量应该是什么数据类型 我需要它用于我们研究所目前正在开发的操作系统 它被称为 ICS OS 我不太清楚具体细节 它在 x86 32 位机器上运行
  • 为什么我收到“找不到编译动态表达式所需的一种或多种类型。”?

    我有一个已更新的项目 NET 3 5 MVC v2 到 NET 4 0 MVC v3 当我尝试使用或设置时编译出现错误 ViewBag Title财产 找不到编译动态表达式所需的一种或多种类型 您是否缺少对 Microsoft CSharp
  • Process.Start 阻塞

    我正在调用 Process Start 但它会阻止当前线程 pInfo new ProcessStartInfo C Windows notepad exe Start process mProcess new Process mProce
  • x86 上未对齐的指针

    有人可以提供一个示例 将指针从一种类型转换为另一种类型由于未对齐而失败吗 在评论中这个答案 https stackoverflow com questions 544928 reading integer size bytes from a
  • 如何使用 std::string 将所有出现的一个字符替换为两个字符?

    有没有一种简单的方法来替换所有出现的 in a std string with 转义 a 中的所有斜杠std string 完成此操作的最简单方法可能是boost字符串算法库 http www boost org doc libs 1 46
  • C 中的异或运算符

    在进行按位操作时 我在确定何时使用 XOR 运算符时遇到一些困难 按位与和或非常简单 当您想要屏蔽位时 请使用按位 AND 常见用例是 IP 寻址和子网掩码 当您想要打开位时 请使用包含或 然而 XOR 总是让我明白 我觉得如果在面试中被问
  • 使用 libcurl 检查 SFTP 站点上是否存在文件

    我使用 C 和 libcurl 进行 SFTP FTPS 传输 在上传文件之前 我需要检查文件是否存在而不实际下载它 如果该文件不存在 我会遇到以下问题 set up curlhandle for the public private ke
  • 使用按位运算符相乘

    我想知道如何使用按位运算符将一系列二进制位相乘 但是 我有兴趣这样做来查找二进制值的十进制小数值 这是我正在尝试做的一个例子 假设 1010010 我想使用每个单独的位 以便将其计算为 1 2 1 0 2 2 1 2 3 0 2 4 虽然我
  • 恢复上传文件控制

    我确实阅读了以下帖子 C 暂停 恢复上传 https stackoverflow com questions 1048330 pause resume upload in c 使用 HTTP 恢复上传 https stackoverflow

随机推荐