我对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
的值。这通常是有符号的 64 位(或 32 位(数字。CharCount
是一个unsigned short
,通常是16位。
实际上,您正在尝试将有符号的 64 位值存储到一个无符号的 16 位值中,而编译器对此不满意。您也应该将CharCount
更改为类型std::streamsize
。
或者,正如 user253751 所建议的那样,由于您知道它将是一个小尺寸 (132(,您可以投射到一个unsigned short
。