0

我必须将函数 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;
}
4

1 回答 1

2

该声明

cout<<setw(10)<<"size of int"<<"= %d" ,sizeof (int)<<endl;

相当于

( (cout<<setw(10)<<"size of int"<<"= %d") , sizeof (int) ) << endl;

请注意那里的逗号 - 它充当逗号运算符,丢弃其左侧参数并返回右侧参数。你得到的基本上是:

sizeof(int) << endl;

这显然是无效的。此外,<< "= %d"只会在流中逐字插入字符串,仅此而已。格式说明符在这里没有意义。当您使用 流式传输时operator<<,重载解析会自动选择正确的重载,以便流式传输的值的格式正确。

你需要

cout << setw(10) << "size of int = " << sizeof(int) << endl;
于 2014-04-20T17:20:49.527 回答