VS Express 没有正确编译代码(?)[关闭]
Posted
技术标签:
【中文标题】VS Express 没有正确编译代码(?)[关闭]【英文标题】:VS Express dosen't compile the code properly (?) [closed] 【发布时间】:2019-11-13 18:04:19 【问题描述】:所以我曾经使用 Code::Blocks IDE,并且非常喜欢它。最近我切换到 Visual Studio。我下载了 VS Express,它是 600mb,我每天只能使用 1gb 数据,我没有 Wi-Fi,所以我这是我唯一的选择。我插入了在 Code::Blocks 中正确编译的相同代码,它需要一些调整才能使其在 VS 中工作,但是当我最终检查它时,输出完全不同,而不是命令行俄罗斯方块,它出现故障并用奇怪的字符填充命令提示符。
这是我稍作调整以使其在 VS 中工作的代码:
#include <iostream>
#include <time.h>
#include <string>
#include <windows.h>
using namespace std;
int nScreenHeight = 30;
int nScreenWidth = 80;
int nFieldWidth = 10;
int nFieldHeight = 25;
unsigned char *pField = NULL;
wstring tetromine[7];
int currentPiece = 0;
int currentRotation = 0;
int currentX = (nFieldWidth/2);
int currentY = 0;
unsigned int score = 0;
int pieceCounter = 0;
int speed = 20;
int speedCounter = 0;
bool forcePieceDown =false;
bool key[4];
bool shiftGridDown = false;
int rotate(int px,int py,int r)
switch(r/90)
case 0:
return py*4+px;//0 degs
case 1:
return 12+py - (px*4);//90 degs
case 2:
return 15 - (py*4) - px;//180 degs
case 3:
return 3 - py + (px*4);//270 degs
return 0;
int doesPieceFit(int id,int rot, int x, int y)
for(int px = 0;px<4;px++)
for(int py = 0;py<4;py++)
int pi = rotate(px,py,rot);
int fi = (y+py) * nFieldWidth + (x+px);
if(x + px>= 0 && x+px < nFieldWidth)
if(tetromine[id][pi] == L'X' && pField[fi]!=0)
return false;
return true;
void lineCheck()
bool line = true;
int lines = 0;
for(int y = 0; y<= nFieldHeight-1;y++)
for(int x = 1; x< nFieldWidth-1;x++)
if(pField[(y)*nFieldWidth+x]!=0)
line &= true;
else line &= false;
if(line) lines++;
if(line)
for(int x = 1; x< nFieldWidth-1;x++)
pField[(y)*nFieldWidth+x] = 8;
int main()
//assets
tetromine[0].append(L"..X.");
tetromine[0].append(L"..X.");
tetromine[0].append(L"..X.");
tetromine[0].append(L"..X.");
tetromine[1].append(L"..X.");
tetromine[1].append(L".XX.");
tetromine[1].append(L".X..");
tetromine[1].append(L"....");
tetromine[2].append(L".X..");
tetromine[2].append(L".XX.");
tetromine[2].append(L"..X.");
tetromine[2].append(L"....");
tetromine[3].append(L"....");
tetromine[3].append(L".XX.");
tetromine[3].append(L".XX.");
tetromine[3].append(L"....");
tetromine[4].append(L"..X.");
tetromine[4].append(L".XX.");
tetromine[4].append(L"..X.");
tetromine[4].append(L"....");
tetromine[5].append(L"....");
tetromine[5].append(L".XX.");
tetromine[5].append(L"..X.");
tetromine[5].append(L"..X.");
tetromine[6].append(L"....");
tetromine[6].append(L".XX.");
tetromine[6].append(L".X..");
tetromine[6].append(L".X..");
pField = new unsigned char[nFieldWidth*nFieldHeight];
for(int x = 0; x<nFieldWidth; x++)
for(int y = 0; y<nFieldHeight; y++)
pField[y*nFieldWidth + x] = (x==0||x==nFieldWidth -1 || y == nFieldHeight - 1) ? 9 : 0;
char *screen = new char [nScreenWidth * nScreenHeight];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
//Display frame
COORD here;
here.X = 0;
here.Y = 0;
WriteConsoleOutputCharacter(hConsole, (LPCWSTR)screen, nScreenWidth * nScreenHeight,here, &dwBytesWritten);
bool gameOver = false;
while(!gameOver)
Sleep(100);
speedCounter++;
if(speedCounter>=speed)
forcePieceDown = true;
speedCounter = 0;
else
forcePieceDown = false;
if(shiftGridDown)
score++;
for(int y = nFieldHeight-2;y > 0;y--)
for(int x = 1;x<nFieldWidth -1;x++)
if((pField[(y)*nFieldWidth+x]) != 0)
pField[(y+1)*nFieldWidth+x] = pField[(y)*nFieldWidth+x];
pField[(y)*nFieldWidth+x] = 0;
shiftGridDown = false;
lineCheck();
for(int x = 1; x< nFieldWidth-1;x++)
if(pField[(nFieldHeight-2)*nFieldWidth+x]==8)
pField[(nFieldHeight-2)*nFieldWidth+x]=0;
if(x==nFieldWidth-2)
shiftGridDown = true;
score+=100;
for(int k = 0;k<4;k++) // R L D Z
key[k] = (0x8000 & GetAsyncKeyState((unsigned char)("DASZ"[k]))) != 0;
if(key[1])
if(doesPieceFit(currentPiece,currentRotation,currentX-1,currentY))
currentX = currentX-1;
else if(key[0])
if(doesPieceFit(currentPiece,currentRotation,currentX+1,currentY))
currentX = currentX+1;
if(key[2])
speedCounter = speed;
if(key[3]&&doesPieceFit(currentPiece,currentRotation+90,currentX,currentY))
(currentRotation+90<=270)?currentRotation+=90:currentRotation=0;
if(forcePieceDown)
if(doesPieceFit(currentPiece,currentRotation,currentX,currentY+1))
currentY++;
else
//lock piece
pieceCounter++;
if(pieceCounter%5==0)
speed-=1;
for(int px = 0;px<4;px++)
for(int py = 0;py<4;py++)
if(tetromine[currentPiece][rotate(px,py,currentRotation)]==L'X')
pField[(currentY+py)*nFieldWidth+(currentX+px)] = currentPiece+1;
score+=20;
//check lines
lineCheck();
//get next piece
currentX = nFieldWidth/2;
currentY = 0;
currentRotation = 0;
srand(time(0));
currentPiece = rand() % 7;
//check game over
gameOver = !doesPieceFit(currentPiece,currentRotation,currentX,currentY);
//draw field
for(int x = 0; x < nFieldWidth; x++)
for(int y = 0; y < nFieldHeight; y++)
screen[(y+2)*nScreenWidth + (x+ 2)] = L" xxxxxxx=#"[pField[y*nFieldWidth + x]];
//draw piece
for(int px = 0;px<4;px++)
for(int py = 0;py<4;py++)
if(tetromine[currentPiece][rotate(px,py,currentRotation)] == L'X')
screen[(currentY+py+2)*nScreenWidth+(currentX+px+2)] = '+';
string s("Score -> ");
string num;
int tmp = score;
while(tmp!=0)
int rem = tmp%10;
tmp /= 10;
num = ((char)(48+rem)) + num;
s+=num;
for(int i = 0; i<s.size();i++)
screen[i] = s[i];
//display frame
WriteConsoleOutputCharacter(hConsole, (LPCWSTR)screen, nScreenWidth * nScreenHeight,here, &dwBytesWritten);
return 0;
这是原始代码:
#include <iostream>
#include <time.h>
#include <string>
#include <windows.h>
using namespace std;
int nScreenHeight = 30;
int nScreenWidth = 80;
int nFieldWidth = 10;
int nFieldHeight = 25;
unsigned char *pField = NULL;
wstring tetromine[7];
int currentPiece = 0;
int currentRotation = 0;
int currentX = (nFieldWidth/2);
int currentY = 0;
unsigned int score = 0;
int pieceCounter = 0;
int speed = 20;
int speedCounter = 0;
bool forcePieceDown =false;
bool key[4];
bool shiftGridDown = false;
int rotate(int px,int py,int r)
switch(r/90)
case 0:
return py*4+px;//0 degs
case 1:
return 12+py - (px*4);//90 degs
case 2:
return 15 - (py*4) - px;//180 degs
case 3:
return 3 - py + (px*4);//270 degs
return 0;
int doesPieceFit(int id,int rot, int x, int y)
for(int px = 0;px<4;px++)
for(int py = 0;py<4;py++)
int pi = rotate(px,py,rot);
int fi = (y+py) * nFieldWidth + (x+px);
if(x + px>= 0 && x+px < nFieldWidth)
if(tetromine[id][pi] == L'X' && pField[fi]!=0)
return false;
return true;
void lineCheck()
bool line = true;
int lines = 0;
for(int y = 0; y<= nFieldHeight-1;y++)
for(int x = 1; x< nFieldWidth-1;x++)
if(pField[(y)*nFieldWidth+x]!=0)
line &= true;
else line &= false;
if(line) lines++;
if(line)
for(int x = 1; x< nFieldWidth-1;x++)
pField[(y)*nFieldWidth+x] = 8;
int main()
//assets
tetromine[0].append(L"..X.");
tetromine[0].append(L"..X.");
tetromine[0].append(L"..X.");
tetromine[0].append(L"..X.");
tetromine[1].append(L"..X.");
tetromine[1].append(L".XX.");
tetromine[1].append(L".X..");
tetromine[1].append(L"....");
tetromine[2].append(L".X..");
tetromine[2].append(L".XX.");
tetromine[2].append(L"..X.");
tetromine[2].append(L"....");
tetromine[3].append(L"....");
tetromine[3].append(L".XX.");
tetromine[3].append(L".XX.");
tetromine[3].append(L"....");
tetromine[4].append(L"..X.");
tetromine[4].append(L".XX.");
tetromine[4].append(L"..X.");
tetromine[4].append(L"....");
tetromine[5].append(L"....");
tetromine[5].append(L".XX.");
tetromine[5].append(L"..X.");
tetromine[5].append(L"..X.");
tetromine[6].append(L"....");
tetromine[6].append(L".XX.");
tetromine[6].append(L".X..");
tetromine[6].append(L".X..");
pField = new unsigned char[nFieldWidth*nFieldHeight];
for(int x = 0; x<nFieldWidth; x++)
for(int y = 0; y<nFieldHeight; y++)
pField[y*nFieldWidth + x] = (x==0||x==nFieldWidth -1 || y == nFieldHeight - 1) ? 9 : 0;
char *screen = new char [nScreenWidth * nScreenHeight];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD dwBytesWritten = 0;
//Display frame
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, 0,0, &dwBytesWritten);
bool gameOver = false;
while(!gameOver)
Sleep(100);
speedCounter++;
if(speedCounter>=speed)
forcePieceDown = true;
speedCounter = 0;
else
forcePieceDown = false;
if(shiftGridDown)
score++;
for(int y = nFieldHeight-2;y > 0;y--)
for(int x = 1;x<nFieldWidth -1;x++)
if((pField[(y)*nFieldWidth+x]) != 0)
pField[(y+1)*nFieldWidth+x] = pField[(y)*nFieldWidth+x];
pField[(y)*nFieldWidth+x] = 0;
shiftGridDown = false;
lineCheck();
for(int x = 1; x< nFieldWidth-1;x++)
if(pField[(nFieldHeight-2)*nFieldWidth+x]==8)
pField[(nFieldHeight-2)*nFieldWidth+x]=0;
if(x==nFieldWidth-2)
shiftGridDown = true;
score+=100;
for(int k = 0;k<4;k++) // R L D Z
key[k] = (0x8000 & GetAsyncKeyState((unsigned char)("DASZ"[k]))) != 0;
if(key[1])
if(doesPieceFit(currentPiece,currentRotation,currentX-1,currentY))
currentX = currentX-1;
else if(key[0])
if(doesPieceFit(currentPiece,currentRotation,currentX+1,currentY))
currentX = currentX+1;
if(key[2])
speedCounter = speed;
if(key[3]&&doesPieceFit(currentPiece,currentRotation+90,currentX,currentY))
(currentRotation+90<=270)?currentRotation+=90:currentRotation=0;
if(forcePieceDown)
if(doesPieceFit(currentPiece,currentRotation,currentX,currentY+1))
currentY++;
else
//lock piece
pieceCounter++;
if(pieceCounter%5==0)
speed-=1;
for(int px = 0;px<4;px++)
for(int py = 0;py<4;py++)
if(tetromine[currentPiece][rotate(px,py,currentRotation)]==L'X')
pField[(currentY+py)*nFieldWidth+(currentX+px)] = currentPiece+1;
score+=20;
//check lines
lineCheck();
//get next piece
currentX = nFieldWidth/2;
currentY = 0;
currentRotation = 0;
srand(time(0));
currentPiece = rand() % 7;
//check game over
gameOver = !doesPieceFit(currentPiece,currentRotation,currentX,currentY);
//draw field
for(int x = 0; x < nFieldWidth; x++)
for(int y = 0; y < nFieldHeight; y++)
screen[(y+2)*nScreenWidth + (x+ 2)] = L" xxxxxxx=#"[pField[y*nFieldWidth + x]];
//draw piece
for(int px = 0;px<4;px++)
for(int py = 0;py<4;py++)
if(tetromine[currentPiece][rotate(px,py,currentRotation)] == L'X')
screen[(currentY+py+2)*nScreenWidth+(currentX+px+2)] = '+';
string s("Score -> ");
string num;
int tmp = score;
while(tmp!=0)
int rem = tmp%10;
tmp /= 10;
num = ((char)(48+rem)) + num;
s+=num;
for(int i = 0; i<s.size();i++)
screen[i] = s[i];
//display frame
WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, 0, 0, &dwBytesWritten);
return 0;
【问题讨论】:
使用调试器。 如果代码行为不同,当使用不同的编译器时,通常,这意味着:你的代码依赖于某种未定义行为的行为。编译器无法正常工作是一个大胆的主张。在 99.99% 的情况下,这种说法是错误的。 由于您在原始代码中添加了(LPCWSTR)
,因此编译器似乎在抱怨screen
的类型错误。如果您不确定它们的作用,请不要添加显式强制转换。此演员表不将窄字符串更改为宽字符串。相反,它将导致未定义的行为。而不是这样做,搜索(如果你没有找到任何东西)询问错误消息的含义。也使用 static_cast
或 reinterpret_cast
代替 C 风格的强制转换。
【参考方案1】:
你有窄和宽字符的混合。在您对WriteConsoleOutputCharacter
的调用中,演员(LPCWSTR)screen
表明有些事情是不正确的。
在这种情况下,screen
是 char
,但您希望它改为 wchar_t
。您已经将wstring
用于tetromine
和L
前缀字符串。您只需要确保其余代码也使用宽字符。
【讨论】:
感谢您的回答。早些时候我使用的是 wchar_t,但后来它在 Code::Blocks 中不起作用。当我将它切换为char时,它起作用了。我现在切换到 wchar_t 它工作了,但它仍然没有正确显示空格,它只是在它的位置显示一个奇怪的字符,你能帮我解决这个问题吗? @Xeno “工作”是一个模糊的术语。更愿意描述预期和收到的行为。 “工作”是指按预期正确编译和执行 这是输出:imgur.com/a/05UEXWa @Xeno 保持一致并始终使用wstring
/wchar_t
/L"..."
或string
/char
/"..."
,但不要混合使用它们。 WriteConsoleOutputCharacter
期望哪一个取决于是否启用了 UNICODE。见docs.microsoft.com/en-us/windows/win32/learnwin32/… 和docs.microsoft.com/en-us/windows/console/…以上是关于VS Express 没有正确编译代码(?)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
Backbone.js vs Express vs Next JS ...和JSP? [关闭]
如何为 VS Express 版本编写 SourceControl 插件 [关闭]