关于ostream以及如何在main函数中使用

about ostream and how to use in the main function

本文关键字:函数 main ostream 关于      更新时间:2023-10-16

我必须写一个函数,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

coutclass ostream的一个对象,表示标准输出流。

你的函数不正确地接受了outstream类型的参数。函数声明:

binary_p(outstream& outs,unsigned int a)

应:

binary_p(std::ostream& outs,unsigned int a)
^^^^^^^^

Do: 还需要告诉程序定义coutcin的名称空间。
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);,然后列出它试图匹配的所有替代方案。