代码之家  ›  专栏  ›  技术社区  ›  Ramazan Chasygov

运算符重载[重复]中没有运算符“===”匹配

  •  1
  • Ramazan Chasygov  · 技术社区  · 8 年前

    我正在研究“罗马到整数”算法,我的代码如下。我收到一个错误:

    no operator "==" matches these operands -- operand types are: char == const Solution::symbol.
    

    有人能帮我修复代码吗?

    // solution.h
    #include <string>
    using namespace std;
    
    class Solution {
      private:
        struct symbol {
          char upperCase;
          char lowerCase;
          bool operator ==(char ch) {
            return ch == upperCase || ch == lowerCase;
          };
        };
        static constexpr symbol one {'I', 'i'};
        static constexpr symbol five {'V', 'v'};
        static constexpr symbol ten {'X', 'x'};
        static constexpr symbol fifty {'L', 'l'};
        static constexpr symbol hundred {'C', 'c'};
        static constexpr symbol fiveHundred {'D', 'd'};
        static constexpr symbol thousand {'M', 'm'};
      public:
        bool romanToInt() {
          char ch = 'I';
          ch == one; // ERROR: no operator "==" matches these operands -- operand types a re: char == const Solution::symbol
          one == ch; // ERROR: no operator "==" matches these operands -- operand types a re: const Solution::symbol == char
        };
    };
    
    // main.cpp
    #include <iostream>
    #include "../Header Files/solution.h"
    using namespace std;
    
    int main() {
      Solution solution;
      solution.romanToInt();
    
      return 0;
    }
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Vlad from Moscow    8 年前

    至少声明运算符为

      bool operator ==(char ch) const {
        return ch == upperCase || ch == lowerCase;
      };
    

    和使用

    return one == ch;