1

I'm working on "Roman to int" algorithm, my code is below. I get an error:

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

Can somebody help me fix the code?

// 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;
}
Ron
  • 14,674
  • 4
  • 34
  • 47
Rami Chasygov
  • 2,714
  • 7
  • 25
  • 37

1 Answers1

2

At least declare the operator like

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

and use

return one == ch;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335