• Register
1 vote



class Student{
    int rolll;

int main(){
    Student x, y;
    x.roll = 33;
    y.roll = 3;

    if(x != y) {
    // do something

I am looking for a solution to why I am getting the “Invalid operands to binary expression c++” error?

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote
Best answer


An expression  x!= y is like a function call to this operator, in fact, it is the same as  x.operator!=(y).

class Student{
    int roll;

    bool operator!=(const Student &other)
        return roll != other.roll;



13 9 6
94,240 points
0 votes


You've abject victim to the most vexing parse, which refers this isanumber is being employed as a function. Take out the parentheses and you must be fine:

int thisisanumber;

Further consider making it a bit more readable, like thisIsANumber. In case you ever require to know it, thisIsANumber uses the camel-case naming convention.

Declare your variable without brackets, see the below statements 

int thisisanumber;

Now, you know with brackets, it is interpreted as a function, and a function can't be passed as a parameter to the >> operator.

Your issue is the reputed most vexing parse. Originally everything, which could be parsed as a function declaration will be parsed as such. Hence the compiler will interpret int thisisanumber(); as a declaration of a function thisisanumber taking zero arguments and returning an int. In case you consider this behaviour the issues with cin>>thisisanumber; must be somewhat selfevident.

In case you remove the parantheses, altering the variable declaration to int thisisanumber;, your program must behave like you'd expect it to with thisisanumber being a variable of type int.

You might but reconsider your naming usage, thisisanumber isn't accurately readable. I would point out going with this_is_a_numberthisIsANumber or ThisIsANumber.

The surplus operator (otherwise known as the modulo operator) % is a binary operator (for example takes accurately 2 operands) and operates just on integer types (for example shortintlonglong long, etc).

It looks from the error message that the variable number1 is of type double. Further, the function pow from the math library returns a value of type double. These two values are the main reason for the error.

There are numerous solutions to this each of which rely on what you want to do. I'll unravel a couple of them:

  1. throwing the operands to ints
foo = (int) bar % (int) quux; // WARNING: you'll be risking to lose data
  1. Declaring number1 to be of an int type and writing your own version of pow that perhaps takes and returns ints
// works only for non-negative integer exponents
int myPow(int n, int exp)
    int result = 1;
    for (; exp > 0; exp--)
        result *= n;

    return result;

// some code

foo = number1 % pow(bar, quux);
  1. Declaring number1 to be of an int type and employing a library function like round, to round the value returned from pow then throwing it to an int

foo = number1 % (int) round(pow(bar, quux));
10 6 4
31,120 points

Related questions

0 votes
1 answer 10 views
I’m getting invalid operands in binary expression in c++. Any idea how to solve?
asked Dec 7, 2020 TeamScript 9.8k points
0 votes
1 answer 133 views
Problem: I am a naïve to programming in C language. Please find below some relevant part of my code. I do not see any issues in my below code but still I keep on receiving below error: “Invalid operands to binary expression ('double' and 'double').” Please find ... is a double???? } printf ("%d\n", rem);          I need the modulo at any condition  but it is not working return 0;
asked Aug 13, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 26 views
I am a newbee. I was doing my coding practice. I was trying to check if a variable exist in a file or not. I wrote some code to do this : def Check(): if Check.my_variable is None: Check.my_variable = >>> Check.my_variable = None but it gives me error : File "main.py", line 3 Check.my_variable = >>> ^ SyntaxError: invalid syntax What is the error in my code and why am I getting this type of error.
asked Mar 14, 2020 LizzyM 6.1k points
2 votes
1 answer 136 views
Problem: Hello guys, I am taking an Udemy online course in C++ recently. I joined a few days back and learning the language step by step. Today, I found a problem in my c++ code that’s stopping my code running. My program is giving back an error “Invalid use ... main.cpp: In function int main()': main.cpp:13:31: error: invalid use of void expression int n = function2(function1()); ^
asked Apr 19, 2020 Gavin 15.3k points
0 votes
2 answers 44 views
Problem: When C++ is working with an operator, it strives to convert the operands to the same type? True False
asked Apr 12, 2020 ArifulIslam 5.6k points
0 votes
1 answer 29 views
Problem : I have been given a small project to write a program in C++ to convert any decimal number into binary numbers. Can anyone help me in writing the code in C++?
asked Oct 8, 2020 sikandar 2.4k points
0 votes
1 answer 70 views
I want to know how to trace the count of 1 in the binary representation.
asked Sep 17, 2020 Daniel Anderson 4k points
0 votes
1 answer 5 views
Problem: No idea what's going on. I looked at other posts that are similar to this issue but no solution helped so far. Here's the code with comments by the parts giving errors. At one point it says that != doesn't work and in the rest of the code it's saying that << isn't working.
asked Dec 23, 2020 Mashhoodch 5.7k points
0 votes
1 answer 111 views
I get an error: IntelliSense: no operator "<<" matches these operands operand types are: std::ostream << std::string c:\Users\sasha\Documents\Visual Studio 2013\Projects\summing a list of number\summing a list of number\summing a list of number.cpp 10 Here's the code: ... Why does it say there is an error because of std and ostream when I have already included iostream and std? I am using VS-2013.
asked Nov 8, 2020 sasha 5.3k points
0 votes
1 answer 6 views
Problem: I get the error invalid conversion from 'const char*' to 'char*'. initializing argument 3 of 'void Printfunc(int, char*, char*)'on argument 3 of the function on the above line. The function is called as shown below void Printfunc(int a, char *loc, char *stream) please let me know if I need to change any initialization.
asked Dec 27, 2020 Mashhoodch 5.7k points