从另一个类访问类的私有实例

时间:2014-11-18 14:29:39

标签: c++ private-members

我是计算机游戏编程的学者,目前正在学习C ++。我正在尝试从.cpp文件访问.h文件中的私有Texture2D和Vector 2类型,以便给出对象位置和图像。

这是Player.cpp文件

#include "Player.h"
#include <sstream>


Player::Player(int argc, char* argv[]) : Game(argc, argv), _cPlayerSpeed(0.1f), _cPlayerFrameTime(250)
{
    //Player Inits
_playerDirection;
_playerFrame = 0;
_playerCurrentFrameTime = 0;
_playerSpeedMultiplier = 1.0f;

//Init of Important Game Aspects
Graphics::Initialise(argc, argv, this, 1024, 768, false, 25, 25, "Genocide: Remastered", 60);
Input::Initialise();
Graphics::StartGameLoop(); //Start of Game Loop, calls Update and Draw in game loop.
}


Player::~Player()
{
}

void Player::Input(int elapsedTime, Input::KeyboardState* state)
{
    // Checks for directional keys pressed
    if (state->IsKeyDown(Input::Keys::D))
    {
        _playerPosition->X += _cPlayerSpeed * elapsedTime;
    }

}



/// <summary> All content should be loaded in this method. </summary>
void Player::LoadContent()
{
    _playerPosition = new Vector2();
    _playerTexture = new Texture2D();
    _playerTexture->Load(" ", false);
    _playerSourceRect = new Rect(0.0f, 0.0f, 0, 0);
}

/// <summary> Called every frame - update game logic here. </summary>
void Player::Update(int elapsedTime)
{

}

/// <summary> Called every frame - draw game here. </summary>
void Player::Draw(int elapsedTime) 
{

}

这是Player.h

#pragma once

#ifdef WIN32 
    #ifndef _DEBUG
        #pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
    #endif
#endif

#include "S2D/S2D.h"
using namespace S2D;

class Player : public Game
{
public:
    Player(int argc, char* argv[]);
    ~Player();
    /// <summary> All content should be loaded in this method. </summary>
    void virtual LoadContent();

    /// <summary> Called every frame - update game logic here. </summary>
    void virtual Update(int elapsedTime);

    /// <summary> Called every frame - draw game here. </summary>
    void virtual Draw(int elapsedTime);

private:
    Vector2* _playerPostion;
    Rect* _playerSourceRect;
    Texture2D* _pacmanTexture;
    const float _cPlayerSpeed;
    const int _cPlayerFrameTime;

    int _playerDirection;
    int _playerFrame;
    int _playerCurrentFrameTime;
    float _playerSpeedMultiplier;

    void Input(int elapsedTime, Input::KeyboardState* state);
    void CheckPaused(Input::KeyboardState* state, Input::Keys pauseKey);
    void CheckViewportCollision();  
    void UpdatePlayer();
};

我实际上已经复制并粘贴了我与Lecturer一起工作的东西,并更改了变量,类型和实例化声明以及他的作品。好奇为什么我的不是。将非常感谢帮助。

非常感谢,

瑞恩。

2 个答案:

答案 0 :(得分:4)

在标题中,您的Texture2D*被称为_pacmanTexture,而在您的实施中,您称之为_playerTexture。同样,您在标题中拼错了_playerPosition

答案 1 :(得分:-1)

通常的方式是将类的私有资源访问另一个类是添加公共访问器方法(getter,setter)。