阶级相互依赖问题

时间:2011-05-01 20:03:09

标签: c++ class dependencies

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

下面是每个类的定义(Map也是一个类):

Sprite类:move()在没有Screen Class定义的情况下不会工作....

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

感谢您的帮助!! =)

2 个答案:

答案 0 :(得分:4)

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

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

应该做什么?这不是你用C ++测试东西的方式。

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

答案 1 :(得分:0)

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