成员未在范围内声明?

发布于 2024-11-15 00:13:10 字数 2725 浏览 3 评论 0原文

因此,在读完一本介绍性书籍后,我正在尝试一些 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

I'm new to this (from had两年的Python),所以我确信我做了某个地方的错误。下面是板创建的相关代码:

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;
}

So I'm trying my hand at some C++ after finishing up an introductory book, and I've become stuck. I've made a vector of objects that each have an SFML circle object as a member, and I want main() to go and draw these circles. The vector is called theBoard, but when I try to access it, I get the following error messages:

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

I'm new to this (came from two years of Python), so I'm sure I made a mistake somewhere. Here is the relevant code for the board creation:

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() is a very, very long function that returns a vector of pointers to Space objects. I doubt there's a problem here, as the only error message I get crops up when I try to access the circle members of Space objects in main(). It seems to me as though I have declared theBoard in the relevant scope, that is, as a data member of the Board class.

My main() function, in case it's important:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白云悠悠 2024-11-22 00:13:10

在您的 main() 函数中,GameBoard 是一个 Board *,而不是 Board。因此,要访问成员,您需要使用 -> 而不是 .。例如:

GameBoard->theBoard.size()

[有些人(我是其中之一)喜欢用前导 pptr 前缀来命名他们的指针变量,以便使这种刺激明显明显。]

In your main() function, GameBoard is a Board *, not a Board. So to access members, you need to use -> instead of .. e.g.:

GameBoard->theBoard.size()

[Some people (I am one of them) like to name their pointer variables with a leading p or ptr prefix, in order to make this kind of irritation explicitly clear.]

像你 2024-11-22 00:13:10

GameBoard 是一个指向 Board 对象的指针,因此您需要使用“->”运算符而不是“.”运算符访问其任何成员变量或方法。

GameBoard is a pointer to a Board object, and thus you need to use the "->" operator instead of the "." operator to access any of its member variables or methods.

趴在窗边数星星i 2024-11-22 00:13:10

如果您仔细阅读,该错误非常明显:

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

The error is quite explicit if you read it carefully:

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

The first line is telling you that you have a pointer to a Board object and you are trying to access a member directly. That is:

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

Also note that GameBoard.*theBoard[i].m_Circle might not be what you want, you probably want (I am guessing as there are important bits missing) something like GameBoard->theBoard[i]->m_Circle.

本王不退位尔等都是臣 2024-11-22 00:13:10

GameBoard 是一个指针,所以语法应该是这样的:

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

由于 theBoard 的元素也是指针,所以我在访问 m_Circle 时使用了指针表示法。

GameBoard is a pointer, so the syntax should be this:

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

Since elements of theBoard also are pointer, so I used the pointer notation when accessing m_Circle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文