Home > OS >  Invalid operands of types 'int' and '<unresolved overloaded function type>'
Invalid operands of types 'int' and '<unresolved overloaded function type>'

Time:06-06

my code;

#include<bits/stdc  .h>
using namespace std;

main(){
    int t;cin>>t;
    while(t--){
        int a;cin>>a;
        if(a==a&(-a)) cout<<a (a==1?2:1)<<endl;
        else cout<<a&(-a)<<endl;
    }
}
Error: s Perfect Bitmasks Classroom.cpp||In function 'int main()':|
s Perfect Bitmasks Classroom.cpp|9|error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

Please can anyone help me fix it.

CodePudding user response:

The problem is simple, and the error message already hinted as to what it is. Line 9 is as follows:

else cout<<a&(-a)<<endl;

Now you probably think that this line is supposed to be parsed as (note extra parentheses) this:

else cout<< (a&(-a)) <<endl;

However, it's being parsed as this:

else (cout<<a) & ((-a)<<endl);

The compiler doesn't know what to do with the expression (-a)<<endl. The left-hand side of the expression, (-a), is an int, while the right-hand side, endl, is what the compiler sees as an <unresolved overloaded function type>, hence the error message.

There are several other problems with your code.

  1. Never use #include <bits/stdc .h>. It is non-standard and only works with some compilers: Why should I not #include <bits/stdc .h>?

  2. The declaration for function main() is wrong. Its return type must be an int. Anything else is non-standard: What is the proper declaration of main in C ?

  3. The expression a==a&(-a) may not do what you want. You may think that it means a == (a&(-a)) (again, note extra parentheses), when instead it means (a==a) & (-a).

  • Related