gpt4 book ai didi

c++ - 错误 C2440 : '=' : cannot convert from 'int *' to 'int **'

转载 作者:太空宇宙 更新时间:2023-11-04 13:13:37 25 4
gpt4 key购买 nike

#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));

关于c++ - 错误 C2440 : '=' : cannot convert from 'int *' to 'int **' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38542640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com