gpt4 book ai didi

c++ - 类相互依赖问题

转载 作者:行者123 更新时间:2023-11-27 23:30:19 25 4
gpt4 key购买 nike

嘿,所以我有三个 2 类:Screen 和 Sprite:它们相互依赖,所以如果我定义一个我不能实现第二个类来定义它,因为它还没有被定义!有没有不需要大量重新编码的简单解决方案?

这里是每个类的定义(Map也是一个类):

Sprite 类:如果没有 Screen 类定义,move() 将无法工作....

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// Sprite Class ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Sprite
{
public:
///////////////////// Get and SET all the privates ///////
Sprite(){};
Sprite(string a_name, char a_symbol, float a_health){
_name = a_name;
_symbol = a_symbol;
_health = a_health;};

char get_symbol() {return _symbol;};
void set_symbol(char _sym) {_symbol = _sym;};

float get_health() {return _health;};
void set_health(float _numb) {_health = _numb;};
void add_health (float _numb) {_health += _numb;};

string get_name() {return _name;};
string set_name(string _aName) {_name = _aName;};

int* get_location(){return _location;};
void set_location(int X, int Y) {
_location[0] = X;
_location[1] = Y;};

//////////////////////////////// Move ////////////
// WONT WORK UNTIL UNLESS SCEEN CLASS IS DEFINED BEFORE IT
bool move(Screen screen,int X, int Y)
{
bool OK = true;
////////////////////// check whats already there /////
char newLoc = screen.get_contents(_location[1]+Y,_location[0]+X);
if (newLoc == '|' || '/' || '_' || '=' || 'X' || 'x' )
OK = false;


if (OK == true)
{
_location[0] += X;
_location[1] += Y;
return true;
}
else
return false;
};
private:
string _name;
char _symbol;
float _health;
int _location[2];
};

屏幕类:(如果没有 Sprite def, Sprite 重载的插入函数将无法工作。

/////////////////////////   SCREEN CLASS        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Screen
{
private:
/////////////////////////////////////////// Screen Variables ///////////////
string _name;
vector <string> _contents;

public:
Screen(string name){_name = name;
_contents.resize(24);};
~Screen(){};

//////////////////////////////////////////// Get contents ///////////////////////////
string get_contents(int Y) {return _contents[Y];};
char get_contents(int X, int Y) {return _contents[Y][X];};

//////////////////////////////////////////// Display (1 FPS) ///////////////////////////
void Display(int numbRefreshes) //
{ //
for(int t = 0; numbRefreshes > t;) //
{ //
UL_2 = GetTickCount()+overSec-UL_1; // Get Time in Milliseconds since start //

// GATE TO GETTING INTO THE DISPLAY FUNCTION (every 1 sec)
if (UL_2 >= 1000) //
{ //
overSec += 1000 - UL_2; //
// Wait another second before update (1000 milliseconds) //
UL_1+=1000; //
UL_3+= 1; //
char UL_3_string[6]; //
itoa(UL_3, UL_3_string, 10); //
// Tell the for loop 1 render is complete (/////)
t+=1; // (///)
// Update the time counter (/)
int B = sizeof(UL_3_string); // |
for(int I = 0; I < sizeof(UL_3_string); I++)
Screen::Insert(UL_3_string[I], 38+I, 22);

/////////////////// Draw each line of the Screen
for (unsigned int I = 0; I < _contents.size(); I++)
{
cout << _contents[I];
}
//////////////// draw any empty lines NOT WORKING WTF???????
for(int emptyLines = 24-_contents.size(); emptyLines > 0; emptyLines--)
{
cout << endl;
}
}
}

};
/////////////////////////////////////////// Insert ////////////////////////
/////////////////// map
bool Insert(Map& _map)
{
for (unsigned int I = 0; I<_map.getContents().size();I++)
{
_contents[I] = _map.getContents()[I];
}
return true;
};
/////////////////// string
bool Insert(string _string, int Y)
{
_contents[Y] = _string;
return true;
};
///////////////////// char
bool Insert(char _char, int X, int Y)
{
_contents[Y][X] = _char;
return true;
};
//////////////////// sprite
bool Insert(Sprite& _sprite)
{
_contents[_sprite.get_location()[0]][_sprite.get_location()[1]] = _sprite.get_symbol();
};

};

屏幕 theScreen("theScreen");

感谢您的帮助!! =)

最佳答案

我会重新设计 - 我不明白为什么 Sprite 需要了解屏幕。这是什么:

  if (newLoc == '|' || '/' || '_' || '=' || 'X' || 'x' )

应该做什么?这不是您在 C++ 中测试事物的方式。

其他问题 - 不要按值传递字符串,将它们作为 const 引用传递,并且名称不要以下划线开头。

关于c++ - 类相互依赖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5850981/

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