我想使用兩個類的變量作爲有權訪問從類A的變量到類B的反之亦然。但是,我無法想出一個可能的解決方案。C++類糾纏編輯
錯誤:它總是在任何一個環,或以下錯誤結束無效使用非靜態數據成員的
下面的代碼示例:
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;
}
已經被敲我的頭靠在牆上這麼我現在的時間...任何想法,將不勝感激!
進行雙向交互通常不是很明智。只提供一個*回調*會更好。 –