首页 > 解决方案 > 我得到一个非法引用和一个不允许的类型名称,为什么?

问题描述

我已经在我的项目中走得更远了,但唉,我又卡住了。我现在从 1 个班级开始运行游戏循环。

我试图让玩家变量在按下按键时在玩家类中更新(参见 game.cpp 中的第 57 至 68 行)。

这些变量保存在私有变量中,然后推送到公共引用中。然后游戏循环应该使用游戏循环中的 Fill() 函数更新玩家位置。

我得到的错误如下:

C2597 非法引用非静态成员 'Player::positionX' game.cpp 第 55 行

C2597 非法引用非静态成员 'Player::positionX' game.cpp 第 55 行

E0254 类型名称不允许,game.cpp 第 30 行

E0254 类型名称不允许,game.cpp 第 30 行

// Game.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "olcConsoleGameEngine.h"
#include "Player.h"



using namespace std;




class GameFunction : public olcConsoleGameEngine{
public:
    GameFunction() {
    }


protected:
    bool OnUserCreate() override {
        return true;

    }

    bool OnUserUpdate(float fElapsedTime) override {
        Fill(0, 0, ScreenWidth(), ScreenHeight(), L' ');

        Fill(Player.RplayerPositionX, 5 * 29, Player.RplayerPositionY, 5 * 29 + 10, PIXEL_SOLID, 5);


        return true;

    }

};


int main()
{



    string name = "stirng";
    
    int var = 0;

    GameFunction game;
    Player player(100, 70, 80);


    game.ConstructConsole(160, 160, 8, 8);
    game.Start();

    //player movement
    switch (cin.get()) {
    case 'a':
        player.playerPositionLeft(10);
        break;
    case 'd':
        player.playerPositionRight(10);
        break;

    default:
        return 0;
        break;
    }


    return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
#pragma once

class Player{
public:
    //constructor
    inline Player(int health, int positionX, int positionY) {
        playerHealth = health;
        playerPositionX = positionX;
        playerPositionY = positionY;

    }
    
    int& RplayerHealth = playerHealth;
    int& RplayerPositionX = playerPositionX;
    int& RplayerPositionY = playerPositionY;


private:
    //player variables
    int playerHealth;
    int playerPositionX;
    int playerPositionY;

public:
    //player functions
    void playerPositionLeft(int num) {
        playerPositionX += num;
        playerPositionY -= num;

    }

    void playerPositionRight(int num) {
        playerPositionY += num;
        playerPositionX -= num;

    }



};

我还能够减少文件和代码量,感谢任何帮助,因为我终于有了一个动人的角色!

github:https ://github.com/JarodIking/Game-C-

标签: c++

解决方案


GameFunction播放器一个引用并让它存储它:

// Inside the GameFunction class
GameFunction(Player& player) : p(player) {}

Player& p;

然后你可以访问pinside的成员OnUserUpdate

bool OnUserUpdate(float fElapsedTime) override {
    Fill(0, 0, ScreenWidth(), ScreenHeight(), L' ');
    Fill(p.playerPositionX, 5 * 29, p.playerPositionY, 5 * 29 + 10, PIXEL_SOLID, 5);
    return true;
}

请注意,代码 as is 不会编译,因为playerPositionXis private. 要么制作它,public要么添加 getter 方法并使用它们。


推荐阅读