关于ostream以及如何在main函数中使用
about ostream and how to use in the main function
我必须写一个函数,binary_print(outstream& outs,unsigned int a)
但是当我为这个函数写一个测试文件时,它说错误。
#include<iostream>
int main()
{
unsigned int d;
std::cout<<"Enter any positive decimal number:";
std::cin>>d;
std::cout<<"Binary of your number is "<<binary_print(cout,d);//<<endl;
std::cout<<'n';}
}
错误:
testfile_rec.cpp:18:60:错误:没有匹配'operator<<' in"std:: operator<& lt;[with _Traits = .std:: char_traits] (((std:: basic_ostream&) (,std:: cout)),((const char *)"你的数字的二进制数是"))<<binary_print (((std:: ostream&) (,Std::cout)), d)' ostream:108:7: note:候选者有:std::basic_ostream<_CharT,Traits>::_ostream_type&std:: basic_ostream<_CharT,_Traits>:: operator<& lt; (std:: basic_ostream<_CharT特征>::_ostream_type&(*)(std::basic_ostream<_CharT,Traits>::_ostream_type&)) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream:117:7: note: std::basic_ostream<_CharT,特征>:_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT,Traits>::_ios_type&(*) (std:: basic_ostream<_CharT特征>::_ios_type&))(与_CharT =char, _Traits = std::char_traits, std::basic_ostream<<em>Traits>::_ostream_type = std::basic_ostream, std::basic_ostream<_CharT,Traits>::_ios_type = .std::basic_ios] ostream:127:7:注:
std::basic_ostream<_CharT,Traits>::_ostream_type&std:: basic_ostream<_CharT _Traits>:: operator<& lt; (std:: ios_base&(*) (std:: ios_base&)) [_CharT = char, _Traits =std:: char_traits, std:: basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream:165:7: note: std::basic_ostream<_CharT,特征>:_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream:169:7: note: std::basic_ostream<_CharT,特征>:_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream:173:7: note: std::basic_ostream<_CharT,特征>:_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream。tcc:91:5:注:std::basic_ostream<_CharT, _Traits>&std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with . int_CharT = char, _Traits = std::char_traits] ostream:180:7:注:std::basic_ostream<_CharT,Traits>::_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int)[with _CharT = char, _Traits = std::char_traits,]std::basic_ostream<_CharT,Traits>::_ostream_type =std:: basic_ostream]上ostream。tcc:105:5:
std::basic_ostream<_CharT, _Traits>&std:: basic_ostream<_CharT,(int) [with _CharT = char, _Traits = std::char_traits] ostream:191:7:注:
std::basic_ostream<_CharT,Traits>::_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with_CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type =std::basic_ostream] ostream:200:7:注:
std::basic_ostream<_CharT,Traits>::_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with_CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type =std::basic_ostream] ostream:204:7:注:
std::basic_ostream<_CharT,Traits>::_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(long long unsignedint) [with _CharT = char, _Traits = std::char_traits,std::basic_ostream<_CharT,Traits>::_ostream_type =std::basic_ostream] ostream:209:7:注:
std::basic_ostream<_CharT,Traits>::_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = . .char, _Traits = std::char_traits, std::basic_ostream<<em>Traits>::_ostream_type = std::basic_ostream] ostream:213:7: note: std::basic_ostream<_CharT,特征>:_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream:221:7: note: std::basic_ostream<_CharT,特征>:_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream:225:7: note: std::basic_ostream<_CharT,特征>:_ostream_type&std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_ostream_type = std::basic_ostream] ostream。tcc:119:5:注:std::basic_ostream<_CharT;_Traits>,std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT,Traits>::_streambuf_type*) [with _CharT = char, _Traits = std::char_traits, std::basic_ostream<_CharT,Traits>::_streambuf_type = std::basic_streambuf]
如果您想要打印给定整数的二进制表示,那么简单的方法是使用std::bitset
作为:
#include<bitset> //include this so as to use std::bitset
unsigned int input;
std::cin >> input;
std::cout << std::bitset<32>(input) << std::endl;
这将打印32位二进制表示存储在input
中的值。这样写更好:
#include <bitset> //for std::bitset
#include <climits> //for CHAR_BIT
std::cout << std::bitset<CHAR_BIT * sizeof(input)>(input) << std::endl;
现在你可以把这个功能包装在一个函数中:
template<typename T>
void binary_print(std::ostream & out, const T & input)
{
out << std::bitset<CHAR_BIT * sizeof(T)>(input) << std::endl;
}
测试代码:
int main() {
int input;
std::cin >> input;
binary_print(std::cout, input); //print int-representation
binary_print(std::cout, (short)input); //print short-representation
return 0;
}
输出:00000000000000000010001000010100
0010001000010100
在线演示:http://ideone.com/OQU6F
cout
是class ostream
的一个对象,表示标准输出流。
你的函数不正确地接受了outstream
类型的参数。函数声明:
binary_p(outstream& outs,unsigned int a)
应:
binary_p(std::ostream& outs,unsigned int a)
^^^^^^^^
Do: 还需要告诉程序定义
cout
和cin
的名称空间。using std::cout;
using std::cin;
在你的c++文件中。我不确定这是唯一的错误,因为你没有在问题中发布实际错误。
你可能不应该在ostream<<
调用中使用它。
cout << "Binary of your number is ";
binary_print(cout,d);
cout << 'n';
编辑:事实上,你现在发布的错误表明编译器无法找出operator<<
与你调用binary_print()的结果一起使用。这个错误基本上是说"error: no match for 'operator<<
' instd::cout<<"Binary of your number is "<<binary_print(cout,d);
,然后列出它试图匹配的所有替代方案。
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 函数是否可以访问传递给main()的参数
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 为什么类和 main() 函数中也有动态内存分配
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 如何在 C 中使用空的 main() 方法运行函数?
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 未解析的外部符号__imp_glewInit函数 main 中引用
- 函数main中的参数argv(int argc,char *argv [])
- 可以(通过编译器)使用多少个线程来初始化全局对象(函数main之前)
- 为什么我的编码出现错误无法在函数 main 中将 int 转换为 int *
- C/C++中函数main()的地址
- 无法在函数 main() 中创建抽象类的指针
- 无法在函数main中编译,需要Lvalue
- 如何在 c++ 中获取主函数"main()"的返回值?