为什么这个函数重载三个整数类型无法编译?

Dar*_*enW 0 c++ overloading compiler-errors

这种为三种大小的整数定义重载函数的尝试失败了.为什么?

byte hack(byte x)
{
   return x+1;
}

unsigned short hack(unsigned short x)
{
   return x+2;
}

unsigned int hack(unsigned int x)
{
   return x+3;
}
Run Code Online (Sandbox Code Playgroud)

编译器告诉我:zzz.cpp:98:错误:重新定义'unsigned int hack(unsigned int)'zzz.cpp:88:错误:'byte hack(byte)'先前在这里定义

And*_*uel 9

您的编译器/代码认为byteunsigned int是相同的东西......

  • @DarenW:可能应该直接使用`std :: uin8_t`.或者`char`如果你不是指八位字节. (2认同)