Car*_*ist 6 c++ class stdvector
我正在尝试构建一个具有字符矩阵的游戏.我正在尝试使用向量向量来构建我的矩阵.我game.h有这个:
#ifndef GAME_H
#define GAME_H
// includes
using namespace std;
class Game
{
private:
int row;
int col;
vector<vector<char>>* matrix;
// other atributtes
public:
Game();
~Game(){}
// some functions
};
#endif
Run Code Online (Sandbox Code Playgroud)
在我的game.cpp:
Game::Game()
{
this->col = 20;
this->row = 20;
// Initialize the matrix
this->matrix = new vector<vector<char>>(this->col);
for(int i = 0 ; i < this->col ; i++)
this->matrix[i].resize(this->row, vector<char>(row));
// Set all positions to be white spaces
for(int i = 0 ; i < this->col; i++)
for(int j = 0 ; j < this->row ; j++)
this->matrix[i][j] = ' ';
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误:
error: no match for ‘operator=’ (operand types are ‘__gnu_cxx::__alloc_traits<std::allocator<std::vector<char> > >::value_type {aka std::vector<char>}’ and ‘char’)
this->matrix[i][j] = ' ';
^~~
Run Code Online (Sandbox Code Playgroud)
在线:
this->matrix[i][j] = ' ';
Run Code Online (Sandbox Code Playgroud)
我想知道是什么导致了这个问题,如何在构造函数中将所有内容设置为空格?
类型this->matrix是std::vector<std::vector<char>>*.
类型this->matrix[i]是std::vector<std::vector<char>>
类型this->matrix[i][j]是std::vector<char>.
因此,
this->matrix[i][j] = ' ';
Run Code Online (Sandbox Code Playgroud)
不起作用.
简化您的代码.更改matrix到
std::vector<std::vector<char>> matrix; // Remove the pointer
Run Code Online (Sandbox Code Playgroud)
相应地调整您的代码.
| 归档时间: |
|
| 查看次数: |
314 次 |
| 最近记录: |