我有一个变量 uint16_t a=35;
我有一个函数
UINT Read(unsigned int& nVal);
如何将 a
作为 unsigned int&
传递给 Read()
?
如果我这样通过
Read(a);
我收到以下错误:
cannot convert parameter 1 from 'uint16_t' to 'unsigned int &
最佳答案
您需要将值复制到(命名的)临时文件中,调用该函数,然后将临时文件复制回来(可能在检查溢出之后)。
uint16_t a = 35;
...
unsigned int temp = a;
const unsigned int result = Read(temp);
// check for overflow here
a = temp;
当然,如果您可以将a
的定义更改为unsigned int
,那就更简单了(但我认为由于其他原因这是不可能的).
关于c++ - 在 C++ 中,如何将 uint16_t 变量传递给带有参数 unsigned int & 的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51190880/