C ++类纠缠编辑

时间:2013-01-11 16:47:35

标签: c++ class

我正在尝试使用两个类的变量,以便从A类变量访问B类,反之亦然。但是,我无法想出一个可能的解决方案。它总是以循环结束,或者出现以下错误:

错误:无效使用非静态数据成员

以下是代码示例:

Player.h:

#ifndef _PLAYER_H_
  #define _PLAYER_H_

#include "Segment/Dynamic_Segment.h"

class Attributes_P;
class Player;

class Attributes_P : public Attributes_DS{
  protected:
  Player *rel;
  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;
  &rel.inv = new int*[this->inv_mcols];
  for( int i = 0; i < this->inv_mrows; i++ ) {
    &rel.inv[i] = new int[this->inv_mcols];
  }
}

void Attributes_P::controls( int MKEY_UP, int MKEY_RIGHT, int MKEY_DOWN, int MKEY_LEFT ) {
  &rel.MKEY_UP = MKEY_UP;
  &rel.MKEY_RIGHT = MKEY_RIGHT;
  &rel.MKEY_DOWN = MKEY_DOWN;
  &rel.MKEY_LEFT = MKEY_LEFT;
}

我的头撞墙已经有一段时间了......任何想法都会受到赞赏!

1 个答案:

答案 0 :(得分:5)

现在我明白了。我认为这是

&rel.

应该是

rel->

rel->MKEY_UP = MKEY_UP;

您的意思是(*rel).MKEY_UP吗?这也有效。