[英]C++ C4716 Beginner error. Understand the error conecpt but not sure how to make the program work
我目前正在為我的一個課程編寫程序,如下所示:
#include <iostream> // std::cout
using namespace std;
#include <windows.h> // SetConsoleCursorPosition(HANDLE,COORD)
#include <conio.h> // _getch()
struct Vector2
{
int x, y;
Vector2() :
x(0), y(0)
{}
Vector2(int x, int y)
{
x = x;
y = y;
}
bool is(int a_x, int a_y)
{
if (a_x == x, a_y==y)
{
return true;
}
return false;
}
};
class Entity //new data class
{
public:
Entity(int x,int y, char i)
{
pos.x = x;
pos.y = y;
icon = i;
}
void setX(int x)
{
pos.x = x;
}
int getX()
{
pos.x;
}
void setY(int y)
{
pos.y = y;
}
int getY()
{
pos.y;
}
void setIcon(char i)
{
icon = i;
}
char getIcon()
{
icon;
}
private:
Vector2 pos;
char icon;
};
enum Gamestate
{
RUNNING, WIN, LOST, USER_QUIT
};
void moveCursor(int x, int y)
{
COORD c = { x,y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int main()
{
// player data
Entity e(3, 4, 1);
//game data
Gamestate state = RUNNING;
int input;
Vector2 size(20, 15);
Vector2 winPosition(size.x / 2, size.y / 2);
do
{ //draw the game world
moveCursor(0, 0);
for (int row = 0; row < size.x; row++)
{
for (int col = 0; col < size.y; col++)
{
cout << '.';
} cout << '\n';
}
//draw player
moveCursor(e.getX(), e.getY());
cout << e.getIcon();
//get input from user
input = _getch();
//process the user's input
switch (input)
{
case 'w': //move up
e.setY(e.getY() - 1);
break;
case 'a': //move left
e.setX(e.getX() - 1);
break;
case 's': //move down
e.setY(e.getY() + 1);
break;
case 'd': //move right
e.setX(e.getX() + 1);
break;
case 27: //quit game
state = USER_QUIT;
break;
}
//show game state
moveCursor(0, size.y + 1);
switch (state)
{
case WIN:
cout << "You WON! Congratulations!\n" ;
break;
case LOST:
cout << "You lost...\n";
break;
}
if (winPosition.is(e.getX(), e.getY()))
{
state = WIN;
}
else
{
state = LOST;
}
} while (state == RUNNING);
//User input ESCAPE to quit program
cout << "Press ESCAPE to quit.\n";
while (_getch() != 27)
;
return 0;
}
錯誤發生於Entity::getY
, Entity::getIcon
, Entity::getX
如果我正確地理解了該錯誤,那是因為沒有從main返回的值? 但是我為修復該問題所做的所有嘗試只會給我留下比以前更多的錯誤。
你有幾個問題。 在您的get函數中,您說應該返回某項,然后再不執行。 另一件事是在struct Vector2
的構造函數中,您說
Vector2(int x, int y)
{
x = x;
y = y;
}
但這不會更改成員變量。 您需要通過更改名稱或使用this
來指定要更改的x
和y
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.