我从 youtube 视频(来自 Husam Haj)中编写了这段代码:
https://www.youtube.com/watch?v=C2k-xiLmNSQ
这段代码应该使用 C++ 动画文本(参见视频),但问题是这段代码不起作用,因为我从 Visual Studio 2017 收到以下消息:
“错误 C2397:从 'int' 到 'SHORT' 的转换需要缩小转换”
我在互联网上进行研究,发现有人已经用相同的代码寻求帮助,但没有解决方案:
http://www.dreamincode.net/forums/topic/358635-i-need-some-help/
我将不胜感激任何帮助
下面是代码:
#include "stdafx.h"
#include <string>
#include <string.h>
#include <windows.h>
#include <iostream>
using namespace std;
void Display(string[], const int);
int main()
{
const int arr_size = 12;
string myInfo[arr_size] = { " Animated text example",
" Brooklyn College",
" Pre-Engineering Program",
};
Display(myInfo, arr_size);
return 0;
}
void Display(string arr_string[], int arr_size)
{
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos[18] = { { 0,6 },{ 0,7 },{ 0,9 },{ 0,11 },{ 0,13 },{ 0,15 },
{ 0,17 },{ 0,19 },{ 0,21 },{ 0,23 },{ 0,25 },{ 0,27 },
{ 0,29 },{ 0,31 },{ 0,33 },{ 0,35 },{ 0,37 },{ 0,39 } };
for (int k = 0; k<arr_size; k++)
{
for (int j = arr_string[k].size(); j >= 0; j--)
{
for (int i = 0; i <= j; i = i++)
{
SetConsoleCursorPosition(screen, pos[k]);
cout << " " << endl;
pos[k].X++;
SetConsoleCursorPosition(screen, pos[k]);
cout << arr_string[k][j] << endl;
Sleep(1);
}
pos[k] = {0,k+1}; //HERE THE MISTAKE!
}
}
}
最佳答案
提示是你有一个 32 位的数字被塞进一个 16 位的空间,并且可能会遭受数据丢失。本以为这是一个警告,而不是编译器错误。
我不知道强制 an integer literal 的后缀到一个 16 位整数,所以一个快速而俗气的解决方案是强制转换
pos[k] = {(SHORT)0,(SHORT)(k+1)};
你会有类似的问题
for (int j = arr_string[k].size(); j >= 0; j--)
因为您从
size()
分配无符号整数为有符号整数。for (size_t j = arr_string[k].size(); j >= 0; j--)
应该解决这个问题。当我们在这里时,请注意
for (int i = 0; i <= j; i = i++)
因为你不知道你是先得到增量(
i++
)还是赋值( i=
)。
关于C++ 文本动画错误 C2397,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43149829/