0

I'm making a card game and I have a few classes. I have a Hand class, a Player Class, a "Column" class (where the cards are placed on the screen after the hand) and I need each class to have access to the other classes' variables.

class Hand
{
private:
    int **Hx,Hy;** //Hand X, Hand Y
    int HAmount;//Amount of cards in Hand
    int HOwner; //Player 1/2
    int Limit; //Limit of cards in Hand
    int HContents[8]; //Card Position in 54 card deck NOT card value.
    bool Removed;
public:
    Hand();
    void Lim();
    void Get_Card();
    void Show();
    void Set_Values(int y, int Own);
};

Then in another class I need to have access to some of the variables above.

void Card::show()
{
    if((apply == true)
    {
        if((Track == true)&&(SelNum == TNum)&&(TOwner == COwner))
        {
            ScnPos = TAmount;
            x = Tx;
            y = Ty + ScnPos*10;
        }
        if((Hand == true)&&(**HOwner** == COwner))
        {

            x = **Hx** + ScnPos*45;
            y = **Hy;**
        }
        apply_surface(x,y,Cards,Screen,&Clip[Pos]);
    }
}

I've tried using class friendship and other methods but I can't make it work. (obviously I have more variables which need this same treatment) (ignore any errors in my code)

4

2 回答 2

2

您的代码中的错误是这里真正的问题。a 没有理由Card访问 aHand的私有成员。这是设计错误,您的其他问题只是想告诉您。

于 2013-04-28T01:14:21.173 回答
0

Well, you should make getters and setters for your variables. E.g.:

class Test {
    private: int a;
    public: int GetA() {
       return this->a;
    }
    void SetA(int a) {
       this->a = a;
    }
}
于 2013-04-28T01:08:54.517 回答