Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将浮点转换成C++中的无符号int?_C++_Unsigned Integer - Fatal编程技术网

如何将浮点转换成C++中的无符号int?

如何将浮点转换成C++中的无符号int?,c++,unsigned-integer,C++,Unsigned Integer,我想将浮点转换为无符号整数。使用此代码时,终端打印3: #include <iostream> int main() { float test = 3.4; std::cout << "test : " << int(test) << "\n"; } 但当我使用此代码时,我有一个错误: #include <iostream> int main() { float test = 3

我想将浮点转换为无符号整数。使用此代码时,终端打印3:

#include <iostream>

int main() {
  float test = 3.4;
  std::cout << "test : " << int(test) << "\n";
}
但当我使用此代码时,我有一个错误:

#include <iostream>

int main() {
  float test = 3.4;
  std::cout << "test : " << unsigned int(test) << "\n";
}
我的错误是:

example.cc: In function ‘int main()’:
example.cc:5:29: error: expected primary-expression before ‘unsigned’
    5 |   std::cout << "test : " << unsigned int(test) << "\n";
      |    
有人能帮我吗?我需要使用一个无符号int。 谢谢

函数型cast typexpr,即要求将类型拼写为单个单词,不带空格、[]、*、&,等等

unsigned int不是一个单词,因此不允许使用

你的选择是:

使用常规C样式转换:unsigned inttest

省略int.unsigned单独表示与unsigned int相同,因此可以编写unsignedtest

使用静态\u转换:静态\u转换测试

函数型cast typexpr,即要求将类型拼写为单个单词,不带空格、[]、*、&,等等

unsigned int不是一个单词,因此不允许使用

你的选择是:

使用常规C样式转换:unsigned inttest

省略int.unsigned单独表示与unsigned int相同,因此可以编写unsignedtest

使用静态\u转换:静态\u转换测试


函数样式强制转换或更好的未签名测试:静态\u强制转换测试。他们两个都是四舍五入的,很有效!谢谢也可以考虑PrtfFungSigNeDestType用于函数类型CAST或更好:STATICE-CasTest.他们两个都是四舍五入的,很有效!谢谢也可以考虑PrtfFCAN,如果它是TyPulf,你会对一个未签名的int做一个函数转换吗?“DAYES是的,你可以,因为TyPulf本身将被拼写成一个单词。如果TyBufff,你能把函数转换成一个未签名的int吗?”DayYes,你可以,因为TyPulf本身将被拼写成一个单词。