C++Visual Studio 将类方法解释为静态



Visual Studio将类方法解释为静态,但它们不是。当我构建下面的代码时,我有 110 个错误。

错误

:有错误的照片1.,有错误的照片2。

可能错误地声明了这些方法?

//header    
class Player {
    public:
        Player(int x, int y) {
            PlayerX = x;
            PlayerY = y;
        };
        void doAction(int input, Mapa *mapa);
        int getDirection();
        Vector2 getPosition();
        int 
PlayerX, PlayerY, direction;
        void turn(int dir);
        void move(int move, Vector2 mapSize, Mapa *mapa);
        Vector2 getCordInFrontOfCharacter();
        Vector2 getCordBehindCharacter();
    };

和 cpp 文件:

#include "Vector2.h"
#include "Player.h"
#include "Mapa.h"
using namespace std;

int PlayerX = 0, PlayerY = 0;
int direction = 0;

void Player::doAction(int input, Mapa *mapa) {
    if (input == (char)72)
        this->move(1, *mapa->mapSize, mapa);
    else if (input == (char)80)
        this->move(-1, *mapa->mapSize, mapa);
    if (input == (char)75)
        this->turn(-1);
    else if (input == (char)77)
        this->turn(1);
}
void Player::turn(int dir) {
    if (dir < 0)
        dir = 2 - dir;
    direction = (direction + dir) % 4;
}
void Player::move(int move, Vector2 mapSize, Mapa *mapa) {
    if (
        move = 1 
        && getCordInFrontOfCharacter().y - 1 >= 0
        && getCordInFrontOfCharacter().x - 1 >= 0
        && getCordInFrontOfCharacter().y - 1 < mapSize.y
        && getCordInFrontOfCharacter().x - 1 < mapSize.x
        && mapa->_Mapa[getCordInFrontOfCharacter().y - 1][getCordInFrontOfCharacter().x - 1] == '0') {
        if (this->direction == 0)
            this->PlayerY -= move;
        else if (this->direction == 2)
            this->PlayerY += move;
        else if (this->direction == 1)
            this->PlayerX += move;
        else if (this->direction == 3)
            this->PlayerX -= move;
        if (this->PlayerY < 1)
            this->PlayerY = 1;
        if (this->PlayerX < 1)
            this->PlayerX = 1;
        if (this->PlayerY > mapSize.y)
            this->PlayerY = mapSize.y;
        if (this->PlayerX > mapSize.x)
            this->PlayerX = mapSize.x;
    }
    else if (   
        move = 1 
        && this->getCordBehindCharacter().y - 1 >= 0
        && this->getCordBehindCharacter().x - 1 >= 0
        && this->getCordBehindCharacter().y - 1 < mapSize.y
        && this->getCordBehindCharacter().x - 1 < mapSize.x
        && mapa->_Mapa[this->getCordBehindCharacter().y - 1][this->getCordBehindCharacter().x - 1] == '0') {
        if (this->direction == 0)
            this->PlayerY -= move;
        else if (this->direction == 2)
            this->PlayerY += move;
        else if (this->direction == 1)
            this->PlayerX += move;
        else if (this->direction == 3)
            this->PlayerX -= move;
        if (this->PlayerY < 1)
            this->PlayerY = 1;
        if (this->PlayerX < 1)
            this->PlayerX = 1;
        if (this->PlayerY > mapSize.y)
            this->PlayerY = mapSize.y;
        if (this->PlayerX > mapSize.x)
            this->PlayerX = mapSize.x;
    }
}
int Player::getDirection() {
    return this->direction;
}
Vector2 Player::getPosition() {
    return Vector2(this->PlayerX, this->PlayerY);
}
Vector2 Player::getCordInFrontOfCharacter() {
    if (this->direction == 2)
        return Vector2(this->PlayerX, this->PlayerY + 1);
    else if (this->direction == 0)
        return Vector2(this->PlayerX, this->PlayerY - 1);
    else if (this->direction == 3)
        return Vector2(this->PlayerX - 1, this->PlayerY);
    else if (this->direction == 1)
        return Vector2(this->PlayerX + 1, this->PlayerY);
    return Vector2(0, 0);
}
Vector2 Player::getCordBehindCharacter() {
    if (direction == 2)
        return Vector2(this->PlayerX, this->PlayerY - 1);
    else if (this->direction == 0)
        return Vector2(this->PlayerX, this->PlayerY + 1);
    else if (this->direction == 3)
        return Vector2(this->PlayerX + 1, this->PlayerY);
    else if (this->direction == 1)
        return Vector2(this->PlayerX - 1, this->PlayerY);
    return Vector2(0, 0);
}

感谢您的帮助。

Visual Studio compilater 显然对这个函数签名感到困惑

 void Player::move(int move, Vector2 mapSize, Mapa *mapa) 

其中,参数和函数本身使用相同的名称。更改它以使它们与众不同:

 void Player::move(int move_, Vector2 mapSize, Mapa *mapa) 
                        // ^

在 cpp 文件中,我用"::"初始化非静态方法

在 c++ 中初始化方法几乎是不可能的。我是说班上的人,嗯,那就错了。

如果您有非静态成员,则无法执行此操作:

int foo::member = 0; // error, member is non-static

每个foo对象都有自己的int member,所以它们之间不共享。您必须为构造函数中的每个对象设置它(也许默认值就是您要查找的(:

foo::foo (int _mem = 0, ... ) : member(_mem) {...}

最新更新