我必须重写fucntion p1()作为p2()以完全模仿p1(),仅使用<iomanip>
并且我不断收到状态错误
类型为
long long unsigned int
且“未解析的函数类型”的无效操作数为二进制operator<<
代码在这里:
void p1()
{
printf("Size of different basic C++ data type in number of bytes\n\n") ;
printf("size of int = %d \n", sizeof (int) ) ;
printf("size of long = %d \n", sizeof (long) ) ;
printf("size of short = %d \n", sizeof (short) ) ;
printf("size of unsigned int = %d \n", sizeof (unsigned int) ) ;
printf("size of char = %d \n", sizeof (char) ) ;
printf("size of wchar_t = %d \n", sizeof (wchar_t) ) ;
printf("size of bool = %d \n", sizeof (bool) ) ;
printf("size of float = %d \n", sizeof (float) ) ;
printf("size of double = %d \n", sizeof (double) ) ;
printf("size of long double = %d \n", sizeof (long double) ) ;
printf("size of int ptr = %d \n", sizeof (int *) ) ;
printf("size of double ptr = %d \n", sizeof (double *) ) ;
printf("size of char ptr = %d \n", sizeof (char *) ) ;
printf("====================================\n\n") ;
}
这是p2(),我必须使用<iomanip>
:
void p2()
{
cout<<"Size of different basic C++ data type in number of bytes\n\n";
cout<<setw(10)<<"size of int"<<"= %d" ,sizeof (int)<<endl;
cout<<setw(10)<<"size of long"<<"= %d" ,sizeof (long)<<endl;
cout<<setw(10)<<"size of short"<<"= %d", sizeof (short)<<endl ;
cout<<setw(10)<<"size of unsigned int"<<"= %d", sizeof (unsigned int)<<endl;
cout<<setw(10)<<"size of char"<<"= %d", sizeof (char)<<endl;
cout<<setw(10)<<"size of wchar_t"<<"= %d", sizeof (wchar_t)<<endl;
cout<<setw(10)<<"size of bool"<<"= %d", sizeof (bool)<<endl;
cout<<setw(10)<<"size of float"<<"= %d", sizeof (float)<<endl;
cout<<setw(10)<<"size of double"<<"= %d", sizeof (double)<<endl;
cout<<setw(10)<<"size of long double"<<"= %d", sizeof (long double)<<endl;
cout<<setw(10)<<"size of int ptr"<<"= %d", sizeof (int *)<<endl;
cout<<setw(10)<<"size of double ptr"<<"= %d", sizeof (double *)<<endl;
cout<<setw(10)<<"size of char ptr"<<"= %d", sizeof (char *)<<endl;
cout<<setfill('=')<<setw(40)<<"="<<endl;
}
答案 0 :(得分:2)
声明
cout<<setw(10)<<"size of int"<<"= %d" ,sizeof (int)<<endl;
相当于
( (cout<<setw(10)<<"size of int"<<"= %d") , sizeof (int) ) << endl;
注意那里的逗号 - 它充当comma operator,丢弃其左手参数并返回正确的参数。你得到的基本上是:
sizeof(int) << endl;
显然无效。此外,<< "= %d"
只会在流中插入字符串,而不是其他内容。格式说明符在这里没有意义。当您使用operator<<
进行流式传输时,重载决策会自动选择正确的重载,以便正确格式化流式传输的值。
你需要
cout << setw(10) << "size of int = " << sizeof(int) << endl;