Search code examples
c++stringtokentokenize

Why do i keep getting this error when comparing string tokens to another string?


This simple function is meant to check if the string tokens passed are operators or not, if they are return true.

bool isOp(std::string tokens){
    for (int i = 0; i < tokens.size(); i++) {
         if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/"){
             return true;
         }

    }

}

However it won't compile and i get an unexpected error that says "ISO C++ forbids comparison between pointer and integer [-fpermissive]", what i don't understand is that i'm not comparing a pointer to an integer, i'm comparing a string to a string? So why do i get this error and how can i fix it?


Solution

  • You're trying to compare single characters, not strings. Replace all your " with ' to make them character constants instead of string literals.