无法用2D矢量成员引用对象

Unable to reference object with 2d vector member

本文关键字:成员 引用 对象 2D      更新时间:2023-10-16

所以我有一个具有2D矢量成员kBoard的类Board。我正在尝试使用std::vector::at()访问kBoard的元素我创建了这样的对象:

Board * board = new Board();

然后,我以这种方式访问成员:

board->kBoard.at(pos0).at(pos1);

在这里,pos0和pos1是整数。
编译器告诉我left of .at must have class/struct/union, type is std::vector<_Ty> [8][8], with _Ty = int

这是我定义 class Board的文件:
board.cpp

#include "Board.h"
Board::Board(void)
{
    for(int i = 0; i < 8; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            kBoard[i][j].assign(1,-1);
        }
    }
}

Board::~Board(void)
{}

board.h

#pragma once
#include <vector>
class Board
{
public:
    Board(void);
    std::vector<int> kBoard[8][8];
    ~Board(void);
};

现在,当我将kBoard定义为整数阵列时,我并没有遇到麻烦,但是当我意识到,如果我想要界限检查,我会做到std::vector<int>,我会做的,我将需要at()std::vector的功能。

这一切对我来说都是正确的,因此,如果错误实际上源于我的代码中的其他位置,我也将完整地粘贴我的主.CPP文件。请记住,此代码实际上没有做任何事情,我只想在尝试编写其余代码之前修复错误。
knightstour.cpp

#include    "KnightsTour.h"
void main()
{
    using namespace std;
    int xPos, yPos, pos1 = 0, pos0 = 0;
    Board * board = new Board;
    board->kBoard.at(pos0).at(pos1); //issue here
    forward_list<Knight> * route = new forward_list<Knight>;
    route->emplace_front();
    cout << "Knight's starting x position: "; cin >> xPos; xPos -= 1;
    cout << "Knight's starting y position: "; cin >> yPos; yPos -= 1;
    route->begin()->setPos(xPos, yPos);
    route->begin()->setMoves();
    for(int i = 0; i < 8; i++)
    {
        pos0 = route->begin()->moves[i].at(0);
        pos1 = route->begin()->moves[i].at(1);
        if(board->kBoard.at(pos0).at(pos1)) //issue here
            ;
    }
    cout << endl;
    delete route;
    delete board;
    return;
}

knightstour.h

#pragma once
#include    <iostream>
#include    <stdlib.h>
#include    <stack>
#include    <forward_list>
#include    "Board.h"
#include    "Knight.h"

我实际上错过了2D向量的正确实现。@nathanoliver节目的评论如下:
一样,一个2D向量 std::vector<std::vector<type>> name
我制作的是一个矢量的2D阵列。