bitwise

Can somebody give a guidance for this code please?
I had an error at line 19 and I have no idea how to fix it.

#include <iostream>
using namespace std;

void printscreen(int x){

cout << "print on screen " << x << endl;
}

float printscreen(int x, int k){

float y = x/2;
return y;
}


int main(){


cout << "y=" << printscreen(9); // << endl;
printscreen(100);

system("pause");
return 0;
}
cout << "y=" << printscreen(9);
This is likely not what you wanted. At the best, this is going to print out the address of this function. Or it just won't compile. printscreen(x) doesn't return anything. The std::cout::operator<< is expecting some value it can output.

Also, in this situation it is not the bitwise shift operator.
You could change this line:
 
    cout << "y=" << printscreen(9);

to
 
    cout << "y=" << printscreen(9,0);


Function printscreen() is "overloaded", that is there are different versions of the function having a different parameter list. By adding the second parameter, the other function will be called.
Thanks guys.

Yeah, Chervil that is work around, like you cheat the program.
Actually I figure it out this:

Functions like int, float, functions does return something, has to be called with "cout".
Functions like void, does not return something has to be called without "cout".

Am I right?
Last edited on
What I suggested was not "cheating".
The code as posted was not correct - you already knew that. Without any background information about (a) where the code originally came from (maybe from a book, or another website, or a college question...) and (b) what the program was supposed to actually do, then my suggestion was a reasonable one.

As for your other comments. yes, you are mostly correct. A function with a type of void is explicitly stating that there will be nothing returned from it.

Though this is somewhat overstating things: 'Functions like int, float, functions does return something, has to be called with "cout".'

There is no "has to", you could call the function without any cout at all.

Let's say you have a function like this:
 
    int twice(int x) {     return 2 * x;    }


You could correctly do any of these:
 
    cout << twice(5);

or
1
2
   int result = twice(5);
   cout << result;

or simply
 
    twice(5);

The last one isn't very useful, but it is valid.

Also finally may I ask, where did the code come from, and what is it supposed to do?

Topic archived. No new replies allowed.