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.
Never use
#include <bits/stdc .h>
. It is non-standard and only works with some compilers: Why should I not #include <bits/stdc .h>?The declaration for function
main()
is wrong. Its return type must be anint
. Anything else is non-standard: What is the proper declaration of main in C ?The expression
a==a&(-a)
may not do what you want. You may think that it meansa == (a&(-a))
(again, note extra parentheses), when instead it means(a==a) & (-a)
.