Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does std::cout << std::cin do?

Tags:

c++

I am new to C++ and was messing around with some of the things that I've learnt. So I tried the following code:

#include <iostream>
int main() {
    std::cout << std::cin;
}

So I expected the code to return an error, but instead got what I think is a memory address (0x6fccc408). Also, when running it multiple times, I got the same memory address, even after restarting cmd. What exactly does this memory address signify?

like image 746
ratorx Avatar asked Aug 10 '16 15:08

ratorx


People also ask

What does std :: cout do in C++?

std::cout is used to output a value (cout = character output) std::cin is used to get an input value (cin = character input)

What is the meaning of std :: cin in C++?

The "c" in C++ cin refers to "character" and "in" means "input". Thus, cin means "character input". The C++ cin object belongs to the istream class. It accepts input from a standard input device, such as a keyboard, and is linked to stdin, the regular C input source.

What is the purpose of CIN?

The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file.

Why do I have to use std :: cout instead of cout?

Those are the same thing. The reason you can use cout instead of std::cout is because your program has "using namespace std;" or "using std::cout;" in it.


1 Answers

It looks like you are using C++98 or C++03. In those versions cin could be implicitly converted to a void* so what you are seeing is that conversion.

If you used C++11, which got rid of the implicit void* conversion and instead implemented a explicit conversion to bool this would produce a compiler error like

clang++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp:4:15: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'istream' (aka 'basic_istream<char>'))
    std::cout << std::cin;
    ~~~~~~~~~ ^  ~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:245:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const void *' for 1st argument; take the address of the argument with &
      operator<<(const void* __p)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/system_error:209:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const std::error_code' for 2nd argument
    operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:108:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to '__ostream_type &(*)(__ostream_type &)' (aka 'basic_ostream<char, std::char_traits<char> > &(*)(basic_ostream<char, std::char_traits<char> > &)') for 1st argument
      operator<<(__ostream_type& (*__pf)(__ostream_type&))
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:117:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to '__ios_type &(*)(__ios_type &)' (aka 'basic_ios<char, std::char_traits<char> > &(*)(basic_ios<char, std::char_traits<char> > &)') for 1st argument
      operator<<(__ios_type& (*__pf)(__ios_type&))
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:127:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'std::ios_base &(*)(std::ios_base &)' for 1st argument
      operator<<(ios_base& (*__pf) (ios_base&))
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:166:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'long' for 1st argument
      operator<<(long __n)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:170:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned long' for 1st argument
      operator<<(unsigned long __n)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:174:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'bool' for 1st argument
      operator<<(bool __n)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:178:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'short' for 1st argument
      operator<<(short __n);
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:181:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned short' for 1st argument
      operator<<(unsigned short __n)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:189:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'int' for 1st argument
      operator<<(int __n);
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:192:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned int' for 1st argument
      operator<<(unsigned int __n)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:201:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'long long' for 1st argument
      operator<<(long long __n)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:205:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned long long' for 1st argument
      operator<<(unsigned long long __n)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:220:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'double' for 1st argument
      operator<<(double __f)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:224:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'float' for 1st argument
      operator<<(float __f)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:232:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'long double' for 1st argument
      operator<<(long double __f)
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:270:7: note: candidate function not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to '__streambuf_type *' (aka 'basic_streambuf<char, std::char_traits<char> > *') for 1st argument
      operator<<(__streambuf_type* __sb);
      ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:502:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'char' for 2nd argument
    operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:508:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'char' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, char __c)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:514:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'signed char' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:519:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'unsigned char' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:556:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const char *' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:569:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const signed char *' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:574:5: note: candidate function [with _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const unsigned char *' for 2nd argument
    operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:628:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::basic_istream<char>] not viable: no known conversion from 'ostream' (aka 'basic_ostream<char>') to 'basic_ostream<char, std::char_traits<char> > &&' for 1st argument
    operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/bits/ostream.tcc:321:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>] not viable: no known conversion from 'istream' (aka 'basic_istream<char>') to 'const char *' for 2nd argument
    operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:497:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'std::basic_istream<char>')
    operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/bits/basic_string.h:5325:5: note: candidate template ignored: could not match 'basic_string' against 'basic_istream'
    operator<<(basic_ostream<_CharT, _Traits>& __os,
    ^
/usr/local/bin/../lib/gcc/x86_64-pc-linux-gnu/6.1.0/../../../../include/c++/6.1.0/ostream:539:5: note: candidate template ignored: could not match 'const _CharT *' against 'istream' (aka 'basic_istream<char>')
    operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
    ^
1 error generated.
like image 170
NathanOliver Avatar answered Oct 14 '22 18:10

NathanOliver