簡體   English   中英

C ++圓形結構

[英]C++ Circular Struct

好的,基本上,我在板上有一塊結構,如下所示:

struct piece
{
    int value;
    bool revealed;
    bool canMove(int x, int y)
    {
        //This is where the problem is.
        if (board[x][y].value == 13) //If it's not occupied
            return true;
        else
            return false;
    }
};

//Define the board
piece board[x][y];

這給了我錯誤,例如'board':未聲明的identifier 我怎樣才能解決這個問題? 我試過將板聲明放在該結構之前,但是它只是說那是一個未聲明的標識符。

我意識到我可以只將板子作為參數傳遞,但是我有幾個功能,這將花費比我更多的時間,因此,如果有其他解決方案,請告訴我!

這個問題是有關(但不相同),以質詢

您必須將類定義與其成員函數的實現分離開來,以便編譯器始終知道您在說什么:

struct piece
{
    int value;
    bool revealed;
    bool canMove(int x, int y);  // this is sufficient for the moment
};

piece board[x][y];   // now, compiler knows what a piece is. 

bool piece::canMove(int x, int y) 
{                    // now board is known as well
    if (board[x][y].value == 13) 
        return true;
    else
        return false;
}

聲明canMove在類內部,然后在聲明board之后在外部定義它。 此時,您可以成功參考board

您正在一塊一塊地混合。 這確實是錯誤的設計。

您的方法canMove屬於單件。 因此,其定義應反映以下事實:

bool canMove() const
{
  return value == 13;
}

要獲得想要從2D數組陣列中獲取的內容,請執行以下操作:

board[x][y].canMove()

當然,您可以創建新的類Board來封裝其行為,例如方法canMove帶有2個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM