• Register
1 vote
38 views

Problem:

 

class Student{
public:
    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

2 Answers

1 vote
 
Best answer

Solution:

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

class Student{
public:
    int roll;

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

 

 

13 9 6
94,240 points
0 votes

Solution:

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 38 views
38 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 Raphael Pacheco 4.9k points
2 votes
1 answer 69 views
69 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 Gavin 15.3k points
0 votes
1 answer 24 views
24 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 LizzyM 6.1k points
0 votes
2 answers 29 views
29 views
Problem: When C++ is working with an operator, it strives to convert the operands to the same type? True False
asked Apr 12 ArifulIslam 5.7k points
0 votes
1 answer 6 views
6 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 sikandar 2.4k points