C++ 我的数组代码有什么问题
我试图生成一个数组,它可能与维度无关。我试过这么做C++ 我的数组代码有什么问题,c++,arrays,C++,Arrays,我试图生成一个数组,它可能与维度无关。我试过这么做 #include <iostream> using namespace std; class array3d { public: array3d(size_t* d, int dims) { int all = 1; size_t* dimensions; int* array; for (size_t i = 0; i < dims; i++) { all =
#include <iostream>
using namespace std;
class array3d
{
public:
array3d(size_t* d, int dims)
{
int all = 1;
size_t* dimensions;
int* array;
for (size_t i = 0; i < dims; i++) {
all = d[i];
dimensions = new size_t[dims];
array = new int[all];
std::cout << array[i] << std::endl;
}
}
};
int main()
{
size_t d[6];
d[0] = 2;
d[1] = 3;
d[2] = 4;
d[3] = 2;
d[4] = 3;
d[5] = 4;
array3d arr(d, 6);
return 0;
}
当我编译它时,我只得到一个零数组,我无法找到哪里出错了。有人能帮忙吗?我不能真正理解代码背后的逻辑,但如果您只是想看到打印出来的内容,您可能打算这样做:
array = new int [all];
//write something here first!
array[i] = some_value; <--- Note that this may access past the end of the array
std::cout << array[i] << std::endl;
或者更可能是您想要:
std::cout << d[i] << std::endl;
注1:除非您真的需要数组,否则您应该真正地听0d0a并使用std::vector
注2:您正在进行一些有符号到无符号的比较i另外,std::cout Google:如何使用调试器。对于动态数组,您应该使用std::vector。您打印数组[i],但从不在数组中放入任何内容。您希望发生什么?