错误C2227,C2065,类中的类

sna*_*bar 0 c++

我正在尝试构建我的代码,就像这个主要的< - 游戏< - 播放器.

如果我写在主要:

player* P;
P = new player;

P->move();
Run Code Online (Sandbox Code Playgroud)

一切正常,但在尝试将此代码移入游戏组件时,我遇到了问题.

以下是我需要帮助的game.cpp的部分内容.

#include "game.h"

#include <string>
using namespace std;

game::game(){
    player* P;
    P = new player;
};


void game::playerStuff(){
P->move(); //c2227+C2065
};
Run Code Online (Sandbox Code Playgroud)

这是game.h的一部分

#include "player.h"

class game {

public:

    game();
    void playerStuff();
Run Code Online (Sandbox Code Playgroud)

Mar*_*rio 5

问题很简单.指向player(P)的指针是一个只在构造函数中可见/存在的局部变量.将其添加为类成员,而不是在游戏类的任何位置使用它:

class game
{
    private:
    player *P;
    public:
    game();
    // other stuff ...
}
Run Code Online (Sandbox Code Playgroud)