【问题标题】:Class inter-dependence problems类相互依赖问题
【发布时间】:2011-05-02 04:14:40
【问题描述】:

嘿,所以我有三个 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,精灵重载的插入函数将无法工作。

/////////////////////////   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");

感谢您的帮助!! =)

【问题讨论】:

    标签: c++ class dependencies


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 允许以下划线开头的名称;只有以双下划线开头的名称可能会给您带来麻烦(特别是,潜在的命名空间与特定于编译器的标记发生冲突)。至于它是否是好的风格,这是值得商榷的——但我发现它有助于明确哪些名称是成员变量(因为 m_ 作为前缀太罗嗦了;^))
    • @Jeremy 我建议您再看看 C++ 标准。以单个下划线开头的名称和其他任何名称都保留在全局范围内。以单个下划线和大写字母开头的名称在所有范围内都是保留的。包含(不仅仅是开头)双下划线的名称在所有范围内都是保留的。
    【解决方案2】:

    首先,将您的类拆分为 .h 文件和 .cpp 文件,然后使用前向声明(谷歌,我无法发布链接)。您的代码中还有一些其他错误,您可能会喜欢 Bjarne Stroustrup 的书《Language c++》

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      相关资源
      最近更新 更多