C++ 我得到一个错误:当我试图创建一个新的;“图像”;班
我正试图在我的main中使用此方法读取文件。我有数据,我想从中提取,以创建一个新的图像对象。现在不太确定如何修复它C++ 我得到一个错误:当我试图创建一个新的;“图像”;班,c++,pointers,file-io,constructor,C++,Pointers,File Io,Constructor,我正试图在我的main中使用此方法读取文件。我有数据,我想从中提取,以创建一个新的图像对象。现在不太确定如何修复它 Image readFile(string fileName) { ifstream file; file.open(fileName); int rowCount; int column; if(file.is_open()){ file >> rowCount; file >> column; } int **row
Image readFile(string fileName) {
ifstream file;
file.open(fileName);
int rowCount;
int column;
if(file.is_open()){
file >> rowCount;
file >> column;
}
int **row = new int*[rowCount];
Image i(**row, column); //The debugger stops here and gives the error!!!!!!!!
file.close();
return i;
}
这是图像对象的构造函数
Image::Image(int R = 0, int C = 0) {
if(R < 1 || C < 1) {
*row = new int[10];
column = new int[10];
} else {
*row = new int[R];
column = new int[C];
}
Image::Image(int R=0,int C=0){
if(R<1 | C<1){
*行=新整数[10];
列=新整数[10];
}否则{
*行=新整数[R];
列=新整数[C];
}
}您没有为
*行分配内存。我想我已经为int**row=new int*[rowCount]分配了内存;为行
分配内存的。现在,您所拥有的只是rowCount
未初始化的指针。为什么在readFile和构造函数中有一个名为row的东西?它们是同一个东西吗?另外:构造函数被声明为接收两个整数。您传递它**行,这是您从未初始化过的,因此第一个参数得到一些未定义的值。