Error : no operator " != " matches these operands

user6119874

When instead of operator !=, << is considered, such error can be removed by including <string> header.

But this doesn't seem to work on operator !=.

So, I thought may be this error is due to difference in type of the variables I'm comparing.

So I considered type casting.

Here is what I'm trying to do :

while ( (cin >> value) != 0) { ... }

The type of value is int.

Some options that I considered are:

  1. Static cast : can't be used because we are accepting value at run time.
  2. Dynamic cast : can't use since value isn't a pointer
  3. reinterpret_cast : same

What other options do I have?

You can suggest some alternatives as well but I would like to know how to make it work.

Niall

The resultant value of (cin >> value) is not the contents of value, but rather cin. cin can be evaluated in a boolean context but doesn't compare to the int, hence the compilation error.

You can modify the line to be (using the comma operator);

while ( (cin >> value, value) != 0)

The resultant here is the second operand of the comma operator, hence the value. In turn it will compare with the 0 correctly. Although this now forgoes the validity tests on the cin stream.

Demo.

Whilst a minimal change is presented above, the code itself tends to be obscure and misses some error checking. It is better to refactor the condition and provide more checks for errors in cin;

while ( (cin >> value) && (value != 0) ) {
  //...
}

Demo.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

IntelliSense: no operator "<<" matches these operands

C++ - no operator "<<" matches these operands directory_iterator()

error: no operator "<" matches these operands

C++ - Error - No operator "[]" matches these operands

Error during implementation of operator>> :C++ no operator matches these operands operand types are: std::istream >> const double error

more than one operator "[]" matches these operands

No operator "<<" matches these operands - C++

C++) E0349 no operator matches these operands occured

no operator ">>" matches these operands -- operand types are: std::istream >> const double

Accessing an entry in std::map causes 'no operator "[]" matches these operands'

No operator "<" matches these operands operand types are: double < my_class

no operator ""<<"" matches these operands error

no operator ">>" matches these operands operand types are: std::istream >> double*

No operator "+" matches these operands, aka cannot add Array<double> to double

No Operator = Matches Operands - DX11

no operator matches these operands; operand types are: std::istream >> const char [5]

E0349 no operator ">>" matches these operands

C++ No operator [] matches these operands

Error: no operator "==" matches these operands

No operator ">=" matches these operands error in c++ when using while loop

no operator "=" matches these operands error

No Operator ">>" matches these operands operand types are: std::istream>>int

"no operator >> matches these operands"

No operator "<<" matches these operands error between an object and a string literal

no operator "<<" matches these operands recursive tower of hanoi error

No operator "[ ]" matches these operands C++

no operator "/" matches these operands

Error C++: no operator matches these operands. operand types are: std::ostream << void

E0349 no operator "<<" matches these operands