#ifndef _grid_h
#define _grid_h
#include<string>
using namespace std;
template<typename T>
class grid{
T** main;
public:
grid<T>(){}
grid<T>(int col, int row){
main = new T[col]; //<-this line gives me error C2440:
//'=' : cannot convert from 'int *' to 'int **'
for(int i =0;i<col;i++)
main[i]=new T[row];
}
};
#endif
我想创建自己的Grid类版本。基本上,我想将信息保存在T的二维数组中。我认为这是最有效的方法。现在如何解决该错误?
它需要是
main = new T*[col];
因为main
是指向的指针数组T
。但是有更好的方法来创建二维数组,例如
std::vector<std::vector<T>> main(col, std::vector<T>(row));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句