在 class 的构造函数中初始化由向量组成的矩阵
Initializing a matrix made of vectors inside constructor of class
我正在尝试构建一个具有字符矩阵的游戏。我正在尝试使用向量的向量来构建我的矩阵。我的 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
在我的 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] = ' ';
}
它给我一个错误:
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] = ' ';
^~~
行中:
this->matrix[i][j] = ' ';
我想知道是什么原因造成的,如何在我的构造函数中将所有内容设置为空白?
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] = ' ';
无效。
简化您的代码。将 matrix
更改为
std::vector<std::vector<char>> matrix; // Remove the pointer
相应地调整您的代码。
如果我是你,我会这样做:
在games.hpp中:
#ifndef GAME_H
#define GAME_H
// includes
template <class T>
class Game : public std::vector<std::vector<T>>
{
private:
int row;
int col;
public:
Game();
~Game(){}
// some functions
};
#endif
在games.cpp
template<class T>
Game<T>::Game(int rr=20, int cc=20):
row(rr), col(cc), std::vector<std::vector<T>>(rr, std::vector<T>(cc))
{
//empty body
}
这自然会使您必须访问元素的方式复杂化,但可以通过重载 return 对您要访问的位置的引用的 operator() 轻松完成。注意通过公开继承 std::vector,我们继承了它们所有的运算符和成员函数和变量。因此我们也继承了std::vectorclass中重载的operator[]。因此我们可以通过重载运算符访问任何元素,如下所示:
template<class T>
T& Game<T>::operator()(int rr, int cc){
return this->operator[](rr)[cc];
}
在上面的 return 语句中,第一部分使用参数 rr 调用重载运算符 [],return 是一个向量对象,我们在这个向量对象上调用重载运算符 []再次通过使用参数 'cc' 作为列索引调用它(就像我们对 std::vector object[index] 所做的那样)
有了这个代码肯定看起来优雅和专业:)
我正在尝试构建一个具有字符矩阵的游戏。我正在尝试使用向量的向量来构建我的矩阵。我的 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
在我的 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] = ' ';
}
它给我一个错误:
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] = ' ';
^~~
行中:
this->matrix[i][j] = ' ';
我想知道是什么原因造成的,如何在我的构造函数中将所有内容设置为空白?
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] = ' ';
无效。
简化您的代码。将 matrix
更改为
std::vector<std::vector<char>> matrix; // Remove the pointer
相应地调整您的代码。
如果我是你,我会这样做:
在games.hpp中:
#ifndef GAME_H
#define GAME_H
// includes
template <class T>
class Game : public std::vector<std::vector<T>>
{
private:
int row;
int col;
public:
Game();
~Game(){}
// some functions
};
#endif
在games.cpp
template<class T>
Game<T>::Game(int rr=20, int cc=20):
row(rr), col(cc), std::vector<std::vector<T>>(rr, std::vector<T>(cc))
{
//empty body
}
这自然会使您必须访问元素的方式复杂化,但可以通过重载 return 对您要访问的位置的引用的 operator() 轻松完成。注意通过公开继承 std::vector,我们继承了它们所有的运算符和成员函数和变量。因此我们也继承了std::vectorclass中重载的operator[]。因此我们可以通过重载运算符访问任何元素,如下所示:
template<class T>
T& Game<T>::operator()(int rr, int cc){
return this->operator[](rr)[cc];
}
在上面的 return 语句中,第一部分使用参数 rr 调用重载运算符 [],return 是一个向量对象,我们在这个向量对象上调用重载运算符 []再次通过使用参数 'cc' 作为列索引调用它(就像我们对 std::vector object[index] 所做的那样)
有了这个代码肯定看起来优雅和专业:)