Invalid operands error for addition and integer division?

invalid operands to binary expression c
invalid operands to binary
invalid operands to binary (have 'double' and 'int')
invalid operands to binary % (have 'float' and int)
invalid operands to binary expression ('float' and 'float') c++
invalid operands to binary expression xcode
error: invalid operands of types 'double' and 'double' to binary 'operator
invalid operands to binary expression ('const)

Here is my function. I am wondering why I cant perform simple math operations on the two int variables

int sockMerchant(int n, vector<int> ar) {
int pairs;

for (int i = 1; i <= 100 ; i++) { //for every number ("Color") between 1 and 100
    for (int j = 0; j < n; j++) { //iterate thru array to look for it
        if (ar[j] == i) { //once found,
            for (int k = j; k < n ; k++) { //count how many of that number is there
                if (ar[k] == i) {
                    int count;
                    count++;
                }
            }
        count = (count/2);
        pairs = (pairs+count);
        }
    }
}
return pairs;
}

Here is the error recieved:

solution.cc: In function ‘int sockMerchant(int, std::vector<int>)’:
solution.cc:20:27: error: invalid operands of types ‘<unresolved overloaded 
function type>’ and ‘int’ to binary ‘operator/’
         count = (count/2);
                  ~~~~~^~
solution.cc:21:27: error: invalid operands of types ‘int’ and ‘<unresolved 
overloaded function type>’ to binary ‘operator+’
         pairs = (pairs+count);
                  ~~~~~^~~~~~

In count = (count/2) and pairs = (pairs+count), you aren't referring to the int you declared because it isn't in scope. You're actually referring to the function std::count because you have using namespace std; somewhere in that compilation unit. This is one of the reasons you shouldn't do that.

To fix this you need to declare count in the appropriate scope:

int sockMerchant(int n, vector<int> ar) {
    int pairs;
    int count = 0;

    for (int i = 1; i <= 100 ; i++) { //for every number ("Color") between 1 and 100
        for (int j = 0; j < n; j++) { //iterate thru array to look for it
            if (ar[j] == i) { //once found,
                for (int k = j; k < n ; k++) { //count how many of that number is there
                    if (ar[k] == i) {
                        count++;
                    }
                }
            count = (count/2);
            pairs = (pairs+count);
            }
        }
    }
    return pairs;
}

Classical FORTRAN: Programming for Engineering and Scientific , Only after the program stops normally do we get the error message. unix [48] a. out In addition to genuine exceptions for division by zero, invalid operand (​such as STOP END Unlike an IEEE floating-point divide by zero, this integer divide  With operands of arithmetic or enumeration type, the result of binary plus is the sum of the operands (after usual arithmetic conversions), and the result of the binary minus operator is the result of subtracting the second operand from the first (after usual arithmetic conversions), except that, if the type supports IEEE floating-point arithmetic (see std::numeric_limits::is_iec559),

Place count outside of the for loops, that should solve it.

Practical Usage of MVS REXX, Example 1 uses a subset of REXX to implement additional features. 15.2.1 Specification Aim: Positive integer multiplication or division of two numbers i.e. the message "invalid operand" (together with the operand in error) is displayed if an  Posted: Thu Jul 12, 2012 6:28 pm Post subject: Re: I have a problem to find the cause of "invalid operands to binary %" Well, you can always try larger integer sizes like long and long long. Alternately, if you don't mind writing the remainder function yourself, you can break the number up into pieces and find the remainder using the remainders of the individual pieces.

First off, count was defined in a scope without a value. You then proceed to use it in an expression: count++;

Not only is this undefined behaviour, but count doesn't even exist after the if(ar[k] == i) scope!

Then you decide to use count, and variable that isn't even in the current scope, in another expression: count = (count/2);

TIP: Don't use integers in division. The value gets truncated.

And finally, you use pairs, which has not been initialized in an expression using itself with the previously mentioned undeclared count: pairs = (pairs + count);

TL;DR: Neither count nor pairs are used correctly. Make sure to initialize both of them to some value, in this case 0, and set them at the appropriate scope.

Why do I get the "invalid operands to binary expression" error , The remainder operator (otherwise known as the modulo operator) % is a binary operator (i.e., takes exactly 2 operands) and operates only on integer types  Why addition, Multiplication, division or modulus is not allowed?? If we perform addition, multiplication, division or modulus on ptr_1 and ptr_2, then the resultant address may or may not be a valid address. That can be out of range or invalid address. This is the reason compiler doesn’t allow these operations on valid addresses.

INT33-C. Ensure that division and remainder , Overflow can also occur during two's complement signed integer division when a divide-by-zero error during the division of the signed operands s_a and s_b : #​include <limits.h> void func(signed long s_a, signed long s_b) { signed long I considered adding this 'stupid' NCCE to INT32-C but felt that that rule was long  While it's true that floating point division does not have a remainder, hence fmod , it's unfortunate that the ESP8266 Arduino-core team is pushing their betas onto the official release channel. 👍 1

Getting error “invalid operands of types `float' and `int' to binary , Getting error “invalid operands of types `float' and `int' to binary `operator^'” C++ floating point math to be done for the division and following multiplications. The result of a vector operation is always the same size as the original operands (except in the cases where a scalar and a vector are used.) GLSL only supports square matrices, so the size of two matrices must be equal to multiply them together.

Why only subtraction of addresses allowed and not division/addition , Output: prog.c: In function 'main': prog.c:8:60: error: invalid operands to binary + (​have 'int *' and 'int *') printf("addition:%lu multipicaion:%lu division:%lu\n",  MySQL Integer Division MySQL has another arithmetic operator which is used for integer division. In integer division, the result of the division operation is truncated to the lowest integer. The syntax of the operator is as follows: A DIV B Where A is the dividend, B is the divisor and the result is the quotient, truncated to the lowest integer.

Comments
  • count is going used out of scope.
  • Hello, welcome to Stackoverflow! While your answer is correct and is insightful, it would help if you gave an example on how the OP may solve their problem. Moreover, explaining why moving count outside the loops solves the problem would also improve the quality of your answer.