C ++ C4244 =':从'std :: streamsize'转换为'unsigned short',可能丢失数据;有什么办法吗?

我对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”);
}

`

anhuiaust 回答:C ++ C4244 =':从'std :: streamsize'转换为'unsigned short',可能丢失数据;有什么办法吗?

警告告诉您,您正在尝试存储的值对于尝试放入的容器来说可能太大。cin.gcount()返回类型为std::streamsize的值。这通常是一个带符号的64位(或32位)数字。 CharCountunsigned short,通常为16位。

实际上,您正在尝试将带符号的64位值存储为无符号的16位值,编译器对此不满意。您还应该将CharCount更改为std::streamsize类型。

或者,正如user253751所建议的那样,由于您知道它的尺寸很小(132),因此可以将其强制转换为unsigned short

本文链接:https://www.f2er.com/2991541.html

大家都在问