What does "ostream& os" do or mean in C++?

I have a function like this:

1
2
3
4
5
6
7
8
9
10
void print(ostream& os, int number)
{
     .....
}

int main()
{
     ....................
     print(number)
}


I just added the "......" so signify code. Anyways, I get an error for print(number) stating that the function does not take 1 argument. I understand why it's telling me that.... but I guess my question is: What is "ostream& os" and what does it do in C++?
ostream is an output stream, the & is to pass by reference ( the only way to pass streams to functions )
your function should be called like this: print(cout,number);
It allows to print to standard output ( cout ) or to any other stream ( like files )
http://www.cplusplus.com/doc/tutorial/basic_io/
http://www.cplusplus.com/doc/tutorial/functions2/
http://www.cplusplus.com/reference/iostream/
Someone posted the following elsewhere:

"Well, I actually believe you're talking about <iostream>. It stands for Input Output Stream. It is a file in the standard C++ library that allows you to recieve user input and output text to screen using cin >> and cout <<. You use it in the preprocessor directive #include <iostream>."

Ok so this is understandable. Normanlly at the top of your code you put: #include <iostream>. So, why would you pass this through a function and, in this case, how can I get rid of this error?
Hey Bazzy... thanks for the reply and the links. I guess I just happened to post last post right after you replied. I've taken a look through the links you have provided earlier today and was still having issues with it. I'm not understanding why I get that error.
What is your exact code and the exact error?
I figured it out... thanks:

1
2
3
4
5
6
7
8
9
10
11
void print(ostream& os, int number)
{
     .....
    os << number;
}

int main()
{
     ....................
     print(cuot,number)
}
You had a very dangerous logical error.
1
2
3
4
5
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', 
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 
//....
y = hexNumber[x];
hexAnswer[x] = hexDigits[y]; //y has ASCII code (too much above 15) 
Last edited on
Topic archived. No new replies allowed.