[英]C4244: '+=' : conversion from 'std::streamsize' to 'size_t', possible loss of data
[英]C++ C4244 =': conversion from 'std::streamsize' to 'unsigned short', possible loss of data; any solution?
我对 C++ 相当陌生,我刚刚完成了对类的学习。 我不知道为什么我不断收到此错误 C4244(检查标题)。 我正在使用 Visual Studio 2017 反馈将不胜感激。
//我的程序要求用户输入一个句子`
#include <iostream>
using namespace std
Const short MAX = 132;
class information
{
char sentence[MAX];
short gcount;
public:
unsigned short CharCount;
void InputData();
void showresult();
};
Int main()
{
Information data;
data.InputData();
data.showresult();
return 0;
}
void information::InputData()//member function to enter info
{
cin.ignore(10, '\n');
cout << "Enter your sentence " << endl;
cout << endl;
cin.getline(sentence, sizeof(sentence));
CharCount = cin.gcount(); // this is the problem
}
void information::showresult() //show number of characters
{
cout << " Characters in the sentence:: " << CharCount << endl;
system(“Pause”);
}
`
该警告告诉您,您正在尝试存储一个值,该值对于您尝试放入的容器来说可能太大。 cin.gcount()
返回类型为std::streamsize
cin.gcount()
的值。 这通常是一个有符号的 64 位(或 32 位)数字。 CharCount
是一个unsigned short
,通常是 16 位。
实际上,您正在尝试将一个有符号的 64 位值存储到一个无符号的 16 位值中,编译器对此并不满意。 您也应该将CharCount
更改为std::streamsize
CharCount
类型。
或者,正如 user253751 所建议的那样,因为您知道它将是一个小尺寸(132),所以您可以将其转换为unsigned short
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.