gpt4 book ai didi

c++ - std::cout << std::cin 做什么?

转载 作者:IT老高 更新时间:2023-10-28 22:13:49 25 4
gpt4 key购买 nike

我是 C++ 的新手,并且正在搞乱我学到的一些东西。所以我尝试了以下代码:

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

所以我预计代码会返回错误,但得到的却是我认为的内存地址 (0x6fccc408)。此外,当多次运行它时,我得到了相同的内存地址,即使在重新启动 cmd 之后也是如此。这个内存地址到底意味着什么?

最佳答案

看起来您使用的是 C++98 或 C++03。在那些版本中, cin 可以隐式转换为 void* 所以你看到的是这种转换。

如果您使用 C++11,它摆脱了隐式 void* 转换,而是实现了显式转换为 bool,这将产生类似

的编译器错误
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.

关于c++ - std::cout << std::cin 做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com