3

ですから、入門書を書き終えた後、C ++を試してみましたが、行き詰まってしまいました。それぞれがメンバーとしてSFML円オブジェクトを持つオブジェクトのベクトルを作成しました。main()にこれらの円を描画させたいと思います。ベクトルはと呼ばtheBoardれますが、それにアクセスしようとすると、次のエラーメッセージが表示されます。

error: request for member 'theBoard' in 'GameBoard', which is of non-class type 'Board*'
error: 'theBoard' was not declared in this scope

私はこれに慣れていないので(Pythonの2年間から来ました)、どこかで間違いを犯したと確信しています。ボード作成に関連するコードは次のとおりです。

class Board
{
public:
    //These are the member functions.
    Board();
    ~Board();
    vector<Space*> CreateBoard();
    //This will be the game board.
    vector<Space*> theBoard;
    //These clusters represent the waiting areas for pieces not yet in the game.
    vector<Space*> Cluster1;
    vector<Space*> Cluster2;
    vector<Space*> Cluster3;
private:
    //These integers represent the number of spaces on each row, starting at the top     (which is row [0])
    vector<int> RowNums;
};

Board::Board()
{
    //Fill in RowNums with the right values.
    RowNums.push_back(1);
    RowNums.push_back(17);
    RowNums.push_back(2);
    RowNums.push_back(17);
    RowNums.push_back(1);
    RowNums.push_back(1);
    RowNums.push_back(5);
    RowNums.push_back(2);
    RowNums.push_back(7);
    RowNums.push_back(2);
    RowNums.push_back(11);
    RowNums.push_back(3);
    RowNums.push_back(17);
    RowNums.push_back(4);
    RowNums.push_back(17);
    //Then, create the board.
    theBoard = CreateBoard();
}

CreateBoard()は、Spaceオブジェクトへのポインターのベクトルを返す非常に長い関数です。main()でSpaceオブジェクトのサークルメンバーにアクセスしようとすると、エラーメッセージが表示されるだけなので、ここに問題があるとは思えません。theBoard私は、関連するスコープで、つまり、Boardクラスのデータメンバーとして宣言したように見えます。

重要な場合の私のmain()関数:

int main()
{
    //This sets up the display window.
    sf::RenderWindow App(sf::VideoMode(1200, 900, 32), "Malefiz");

    //This creates the board on the heap, and a pointer to it.
    Board* GameBoard = new Board();

    cout << "Board made.";

    //This is the game loop.
    while(App.IsOpened())
    {
        //This is used to poll events.
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            //This closes the window.
            if(Event.Type == sf::Event::Closed)
            {
                App.Close();
            }
        }

        //This gets the time since the last frame.
        //float ElapsedTime = App.GetFrameTime();

        //This fills the window with black.
        App.Clear(sf::Color(200, 200, 125));
        //This draws the places into the window.
        for(int i = 0; i < GameBoard.theBoard.size(); ++i)
        {
            App.Draw(GameBoard.*theBoard[i].m_Circle);
        }
        //This displays the window.
        App.Display();
    }

    return EXIT_SUCCESS;
}
4

4 に答える 4

6

あなたのmain()関数でGameBoardは、はであり、ではBoard *ありませんBoard->したがって、メンバーにアクセスするには、の代わりにを使用する必要があります.。例えば:

GameBoard->theBoard.size()

[この種の苛立ちを明確にするために、ポインター変数に先頭または接頭辞を付けて名前を付けることを好む人もいます(私もその1人です) 。pptr]

于 2011-06-13T18:11:19.033 に答える
4

GameBoardはBoardオブジェクトへのポインターであるため、「。」の代わりに「->」演算子を使用する必要があります。そのメンバー変数またはメソッドのいずれかにアクセスするための演算子。

于 2011-06-13T18:12:22.693 に答える
3

GameBoardはポインタであるため、構文は次のようになります。

 for(int i = 0; i < GameBoard->theBoard.size(); ++i)
 {
        App.Draw((GameBoard->theBoard[i])->m_Circle);
 }

の要素theBoardもポインタなので、にアクセスするときにポインタ表記を使用しましたm_Circle

于 2011-06-13T18:11:37.227 に答える
3

注意深く読むと、エラーは非常に明白です。

error: request for member 'theBoard' in 'GameBoard', which is of non-class type 'Board*'
error: 'theBoard' was not declared in this scope

最初の行は、オブジェクトへのポインターがありBoard、メンバーに直接アクセスしようとしていることを示しています。あれは:

Board *p = ...
p.theBoard;     // Error, should be p->theBoard, as p is a pointer

また、それGameBoard.*theBoard[i].m_Circleはあなたが望むものではないかもしれないことに注意してください、あなたはおそらく(重要なビットが欠けているので私は推測しています)のようなものが欲しいでしょうGameBoard->theBoard[i]->m_Circle

于 2011-06-13T18:11:47.027 に答える