c++ - 运算符 == 标记为 'override' ,但不覆盖

标签 c++ operator-overloading c++14

我在重写基类运算符==时遇到问题。这是代码。

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
class IClient {
  virtual const std::vector<T>& getID() = 0;
  virtual bool isEqual(const std::vector<T>& anotherID) = 0;
  virtual bool operator==(const IClient& anotherClient) = 0;
};

class SeviceClient : public IClient<int8_t> {
  const std::vector<int8_t> ID;

 public:
  SeviceClient(const std::vector<int8_t>& ID) : ID(std::move(ID)) {}
  SeviceClient(const std::vector<int8_t>&& ID) : ID(std::move(ID)) {}

  const std::vector<int8_t>& getID() override {
      return ID;
  }
  bool isEqual(const std::vector<int8_t>& anotherID) override {
      return true;
  }
  bool operator==(const SeviceClient& anotherClient) override {
      return true;
  }
};

它提示说

 error: 'bool SeviceClient::operator==(const SeviceClient&)' marked 'override', but does not override

当我将函数转换为时,编译器看起来很高兴

bool operator==(const IClient<int8_t>& anotherClient) override {}

这是问题的解决方案还是我在这里遗漏了一些东西?

谢谢

最佳答案

SeviceClient 和 IClient 是不同的类型。 因此, bool SeviceClient::operator==(const SeviceClient&) 没有基本声明。

关于c++ - 运算符 == 标记为 'override' ,但不覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68225394/

相关文章:

c++ - 派生类能够访问其基类的私有(private)成员

后台窗口的 C++ WinAPI 屏幕截图

c++ - 信号 : Segmentation fault (11) Signal code: Invalid permissions (2)

c++ - OpenCV:convertTo 返回白色图像(有时)

c++ - operator[] 歧义消解

c++ - 保留 std::initializer_list 的拷贝是否安全?这是什么道理?

c++ - 实现文件将只识别其他类的前向声明

c++ - 运算符的使用 ->*

c++ - 访问重载的模板函数

c++ - 来自 Boost.Spirit 语法的属性:来自 std:vector of boost::variant 的错误