operator overloading

Hi.Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;


int main()
{
    int c = 0;
    
    cout <<c = 0<< ','<<c==0<< endl;
    cin.get();
    return 0;
}

What's wrong with it?Please give me the exact answer

Thanks in advance
Last edited on
You need to put parentheses around c=0 and c==0 to make it compile.

c = 0 will assign the value 0 to the variable c. Is that really what you want to do?
Yes.I look for the cases of the error.I know how to solve it
It has to do with << having higher precedence than = and ==.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

The compiler sees it as:
(cout <<c) = ((((0<< ',')<<c))==(0<< endl));
Last edited on
The problem is with the errors I get.I don't understand them
Last edited on
You haven't told us what error you get. The output I get when trying to compile your program is:
test.cpp: In function ‘int main()’:
test.cpp:9:20: warning: left shift count >= width of type [enabled by default]
test.cpp:9:32: error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’
Compilation failed.


The first thing is a warning (not an error). The part it warns about is 0<< ','. When << is used on integers it's the left bit shift operator. It returns the value of the left operand with the bits shifted to the left as many positions as the value of the right operand. ',' is 44 in ASCII. 0 is an int which is 32 bits on my compiler. The C++ standard says that the behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand. 44 > 32 so the behavior is undefined.


The error is this part 0<< endl. endl is a function and there is no version of << that takes an int and a function as arguments so it gives an error.
Last edited on
But why do u get the same error when you put parantheses around 'c==0'?
If you do that the compiler sees:
(cout <<c) = (((0<< ',')<<(c==0))<< endl);

((0<< ',')<<(c==0)) returns an int so you still have the same kind of error. An int to the left and endl to the right of <<.
Last edited on
Does it give th same waning when executed on 64-bit computers?
and tell me please, why would the compiler use the phrase 'UNRESOLVED overloaded function ' in the error on L32?
Last edited on
I did compile the program on a 64-bit computer using a 64-bit compiler.

endl is a template function and the compiler hasn't been able to determine which template arguments to use.
Last edited on
Thank you indeed.So you got only one error and no warning?
Last edited on
No, I got the warning and the error.
Why the warning?Now int was 64-bit.right?and, 44<64
No. int is 32 bits on many 64-bit compilers.
Topic archived. No new replies allowed.