C ++ C4716初学者错误。理解错误概念,但不知道如何使程序工作

时间:2018-03-02 07:16:55

标签: c++ compiler-errors

我目前正在为我的一个课程开设一个类似的程序:

#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返回的值,它会发生吗?但是我尝试修复它的所有事情都让我在之前遇到了更多的错误。

1 个答案:

答案 0 :(得分:0)

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

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

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