1

クラスAの変数からクラスBの変数に、またはその逆にアクセスできるように、2つのクラスの変数を使用しようとしています。しかし、私は可能な解決策を見つけることができません。常にループになるか、次のエラーが発生します。

error: invalid use of non-static data member  

コードサンプルは次のとおりです。

Player.h:

  #ifndef _PLAYER_H_
  #define _PLAYER_H_

#include "Segment/Dynamic_Segment.h"

class Attributes_P;

class Attributes_P : public Attributes_DS{
  protected:
  int inv_mcols, inv_mrows;

  public:
  Attributes_P();
  void controls( int MKEY_UP, int MKEY_RIGHT, int MKEY_DOWN, int MKEY_LEFT );
  void inventory( int inv_mcols, int inv_mrows );
};

class Player : public Dynamic_Segment{
  protected:
  int   **inv;

  public:

  int   MKEY_UP, MKEY_RIGHT, MKEY_DOWN, MKEY_LEFT;

  public:

  Player();
  Attributes_P set;
  friend class Core;
  friend class Attributes_P;

};
#endif

Player.cpp:

#include "Segment/Player.h"

Attributes_P::Attributes_P(){};

Player::Player() : Dynamic_Segment(){
  set.inv_mcols = 0;
  set.inv_mrows = 0;
}

void Attributes_P::inventory( int inv_mcols, int inv_mrows ) {
  this->inv_mcols = inv_mcols;
  this->inv_mrows = inv_mrows;
  Player::inv = new int*[this->inv_mcols]; //<--- Error here
  for( int i = 0; i < this->inv_mrows; i++ ) {
    Player::inv[i] = new int[this->inv_mcols]; //<--- Error here
  }
}

void Attributes_P::controls( int MKEY_UP, int MKEY_RIGHT, int MKEY_DOWN, int MKEY_LEFT ) {
  Player::MKEY_UP = MKEY_UP; //<--- Error here
  Player::MKEY_RIGHT = MKEY_RIGHT; //<--- Error here
  Player::MKEY_DOWN = MKEY_DOWN; //<--- Error here
  Player::MKEY_LEFT = MKEY_LEFT; //<--- Error here
}

しばらくの間、壁に頭をぶつけてきました...どんなアイデアでもいただければ幸いです。

4

3 に答える 3

4

メンバー

Player::MKEY_UP
Player::MKEY_RIGHT
Player::MKEY_DOWN
Player::MKEY_LEFT

ではないため、クラスインスタンスではなくstatic、タイプのオブジェクトを介してのみアクセスできます。Player

2つのプレーヤーオブジェクトを作成するp1としp2ます。を呼び出すときAttributes_P::controls、2つのオブジェクトのメンバーのどちらを変更する必要がありますか?p1またはp2

staticこれらのメンバーをオブジェクト間で共有するように宣言するかPlayer、特定のPlayerオブジェクトをパラメーターとして渡してそのメンバーに直接アクセスすることができます。これはロジックの一部であり、選択はプログラムをどのように機能させるかによって異なります。

于 2013-01-11T15:35:49.863 に答える
2

属性MKEY_UP、MKEY_RIGHT、MKEY_DOWN、MKEY_LEFT、およびinvはプライベートであるため、これらにアクセスすることはできません。

それらをプライベートにして、ゲッター/セッターを書いてください!

于 2013-01-11T15:50:18.610 に答える
0

あなたのデータはおそらくあまりにも密接に絡み合っていると思います、そして多分ただ一つのクラスであるべきです。クラスの目的の1つはデータをカプセル化することであるため、他の人のプライベートをいじる必要はありません。newまた、使用していることや配列などに他の問題があります。しかし...

で、のメンバーとして宣言されているAttributes_P::inventoryを変更しようとしています。しかし、あなたがどのプレイヤーを参照しているのかわかりません。あなたはどちらかをする必要がありますinvPlayerAttributes_P

  1. Playerインスタンスをinventory関数に渡す、または
  2. Attributes_Pを参照して初期化しplayerます。

オプション1:

void Attributes_P::inventory(int inv_mcols, int inv_mrows, Player& player) {
    player.inv = ...
}

オプション2:

class Player; // needed so compiler understands next few lines refering to Player

class Attributes_P {
    Player& m_player;
public:
    Attributes_P(Player& player) : m_player(player) {
    }
};

class Player {
    Attributes_P m_attributes;
public:
    Player() : m_attributes(*this) { // pass self to Attributes_P constructor
    }
}

void Attributes_P::inventory(int inv_mcols, int inv_mrows) {
    m_player.inv = ...
}
于 2013-01-11T20:23:40.393 に答える