C++ Class Entanglement
C++ Class Entanglement
我试图使用两个类的变量作为从类A的变量访问类B的,反之亦然。然而,我想不出一个可能的解决方案。它总是以循环或以下错误结束:
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
}
已经有一段时间了…任何想法都会很感激!
成员
Player::MKEY_UP
Player::MKEY_RIGHT
Player::MKEY_DOWN
Player::MKEY_LEFT
不是static
,因此您只能通过类型为Player
的对象访问它们,而不能通过类实例访问它们。
假设你创建了2个播放器对象,p1
和p2
。当调用Attributes_P::controls
时,应该更改两个对象的哪个成员?p1
还是p2
?
Player
对象之间共享这些成员,你可以声明这些成员为static
,或者传递一个特定的Player
对象作为参数并直接访问它的成员。这是逻辑的一部分,选择取决于您希望程序如何工作。
不能访问MKEY_UP, MKEY_RIGHT, MKEY_DOWN, MKEY_LEFT &因为它们是私有的。
将它们设为私有并写入getter/setter!
我认为您的数据可能过于紧密地交织在一起,也许应该只是一个类。类的目的之一是封装数据,这样就不用去摆弄别人的私有数据了。还有其他问题,你正在做什么使用new
和数组等。然而…
在Attributes_P::inventory
中,你试图修改inv
,它被声明为Player
的成员。但是Attributes_P
不知道你指的是哪个玩家。您需要
- 将
Player
实例传递给inventory
函数,或者 - 用
player
的引用初始化Attributes_P
。
选项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 = ...
}
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- C++ class vs a library
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 使用"class"关键字,后跟未声明的标识符
- C++ Class Entanglement