1

I was making a calculator, this is one of my files:

#include <iostream>

float getInfo()
{
    std::cout << "Enter a number: ";
    float input{};
    std::cin >> input;

    return input;
}

char getOperator()
{
    std::cout << "Enter an operator: ";
    char operand{};
    std::cin >> operand;

    while (operand != '+' && operand!= '-' && operand!= '*' && operand!= '/')
    {
        std::cout << "Invalid operator, enter valid operator: ";
        std::cin >> operand;
    }
    return operand;
}

The variable identifier operand used to be operator, however my IDE wouldn't recognize it as a variable. Hence, the question.

  • What specific, exact error were you getting before? – JohnFilleau Nov 05 '20 at 02:01
  • 2
    `operator` is a reserved keyword in C++. As such, it is not available for use in anything not specified as part of the language. Using it as the name of a variable is therefore not allowed. – Peter Nov 05 '20 at 02:06
  • Some of the comments indicate that your use of "it" is ambiguous. When you say "wouldn't recognize it as a variable", is "it" supposed to refer to "operand" or "operator"? – JaMiT Nov 05 '20 at 02:07
  • @yyxy-OEC-1to3 if you are returning this operator as `char`, than it will not work. – pradeexsu Nov 05 '20 at 02:09
  • 1
    To make a question clearer, you should show the code that reproduces the error that you're asking about rather than another example that doesn't have an error. – eerorika Nov 05 '20 at 02:14
  • try searching in a [C++ book](https://stackoverflow.com/q/388242/995714) first, or [cppreference](https://www.google.com/search?q=operator+site:en.cppreference.com) – phuclv Nov 05 '20 at 02:48

2 Answers2

2

operator is a keyword, it is not part of the standard library. Your error is because you can't use keywords as variable names.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

See the list of C++ keywords. The problem is not that the identifier "operator" is part of the standard library, but that it is part of the language itself (even more fundamental than the standard library).

JaMiT
  • 14,422
  • 4
  • 15
  • 31