无法用2D矢量成员引用对象
Unable to reference object with 2d vector member
所以我有一个具有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阵列。
相关文章:
- 非静态成员引用必须相对于特定对象
- 在C++中,如果成员引用在其声明中初始化,为什么需要存储空间?
- 成员引用基类型 'char' 不是 C++ 中的结构或联合
- 成员引用基类型不是结构或联合
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- 成员引用基类型 'double [10]' 不是结构或联合
- 使用默认值初始化成员引用
- 错误:成员引用基类型"uint32_t"(也称为"无符号 INT")不是结构或联合
- 无法用2D矢量成员引用对象
- 是否可以在同一对象中对成员 std::shared_ptr 进行成员引用<variable>?
- 是否有一种方法可以将C 类成员引用到向量而不明确添加它们
- C++:成员引用还是指针
- C++非静态成员引用
- C++ 成员引用基类型'int'不是结构或联合
- 将成员引用变量置于成员指针变量之上的原因
- C++非静态成员引用必须相对于特定对象
- 无法理解错误 msg:"非静态成员引用必须相对于特定对象"
- 返回类成员引用变量会导致运行时错误
- 可以安全地将移动的对象存储在成员引用中
- 成员引用基类型'double'不是结构或联合