gpt4 book ai didi

C++ C4716 初学者错误。了解错误概念但不确定如何使程序运行

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:02 25 4
gpt4 key购买 nike

我目前正在为我的一个类(class)编写程序,程序如下:

#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::getYEntity::getIconEntity::getX 发生错误如果我正确理解了错误,那是因为 main 没有返回任何值?但是我尝试做的所有修复工作都让我犯了比以前更多的错误。

最佳答案

你有几个问题。在你的 get 函数中,你说你应该返回一些东西,然后从不返回。另一件事是在你说的 struct Vector2 的构造函数中

Vector2(int x, int y)
{
x = x;
y = y;
}

但这并没有改变成员变量。您需要通过更改名称或使用 this 来指定要更改的 xy

关于C++ C4716 初学者错误。了解错误概念但不确定如何使程序运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49064378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com