gpt4 book ai didi

C++ 类纠缠编辑

转载 作者:太空宇宙 更新时间:2023-11-04 16:23:36 24 4
gpt4 key购买 nike

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

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

这是代码示例:

播放器.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

播放器.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;
}

一段时间以来我一直在用头撞墙......任何想法将不胜感激!

最佳答案

现在我明白了。我认为是

&rel.

应该是

rel->

rel->MKEY_UP = MKEY_UP;

您是说 (*rel).MKEY_UP 吗?这也有效。

关于C++ 类纠缠编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14282763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com