[英]C++: Copy elements of matrix class to an array
我有此方法(Matrix :: WriteToArray(double&CopyOfArray)),我想將Matrix對象中的數組副本寫入double數組(即CopyOfArray)。 我在編譯時遇到了麻煩。
任何幫助表示贊賞。 謝謝
錯誤:
$ make
g++ -g -Wall -c main.cpp
main.cpp: In function ‘int mrstart(double, double*, Matrix&, Matrix&)’:
main.cpp:459:13: error: ‘cff’ declared as reference but not initialized
main.cpp:465:45: error: invalid type argument of unary ‘*’
main.cpp:467:73: error: invalid type argument of unary ‘*’
main.cpp:470:77: error: invalid type argument of unary ‘*’
Makefile:20: recipe for target `main.o' failed
make: *** [main.o] Error 1
以下是支持文件:Main.cpp
int mrstart(double hcen, double mr[], Matrix &a, Matrix &HT)
{
double *cff;
a.WriteToArray(&cff);
/*...*/
}
矩陣
int Matrix::WriteToArray(double &CopyOfArray){
int i;
for(i=0;i<n_rows;i++){
CopyOfArray[i]=array[i*n_cols];
i++;
}
return *CopyOfArray;
}
矩陣
#ifndef MATRIX_H
#define MATRIX_H
// Matrix class of variable size
class Matrix {
private:
int n_rows;
int n_cols;
double *array;
public:
// Constructors
Matrix(); // default constructor
Matrix(int rows, int cols); // two-argument constructor
// Matrix(const Matrix &arr); // copy constructor
// Destructor
~Matrix();
// Mutators
// void add(Matrix m2);
// void subtract(Matrix m2);
void setelem(int r, int c, double val);
// Accessors
// void add(Matrix m2);
// void subtract(Matrix m2);
int getrows();
int getcols();
double getelem(int r, int c);
bool equals(Matrix m2);
char display();
int WriteToArray(double &CopyOfArray);
};
#endif
你要
int Matrix::WriteToArray(double CopyOfArray[], const int size){
//make sure size >= n_rows then copy
}
並這樣稱呼它
double cff[MAX_SIZE] = {};
a.WriteToArray(cff);
您應該真正使用std :: vector而不用擔心動態分配。
編輯:好的,如果您確實願意,可以進行手動分配,但是要小心釋放它:
double* cff = 0;
a.WriteToArray(cff);
//do stuff with cff
delete [] cff;
在你里面寫功能
int Matrix::WriteToArray(double *dest){
dest = new double[n_rows];
//copy data into dest
}
最主要的是確保在main中使用完dest后將其刪除,以免造成內存泄漏。
double *cff;
a.WriteToArray(&cff);
您要聲明一個指針,然后在初始化之前使用它。 您正在向函數傳遞沒有指向任何內容的指針。 如果在編譯時知道大小,則應該靜態聲明數組
double cff[16]; // 4x4 array, for example
a.WriteToArray(cff);
或在調用函數之前適當調整大小。
double * cff = new double[n_rows * n_cols];
a.WriteToArray(cff);
其他一些批評:您的函數期望引用double作為參數。 如果要接收一個數組,通常的方法是請求一個指針。 更好的方法是根本不使用它們,而使用某種形式的智能指針。
該方法本身也被破壞。
CopyOfArray[i]=array[i*n_cols];
i++;
這將導致您將數組中每一行的第一個元素寫入,並在它們之間留出一個空格。
您需要一個嵌套循環。 您也不應該返回任何內容,因為您已經在寫入參數數組,因此返回值是多余的。 您也不應將指針作為int返回,而應將其作為指針返回。 不過,更好的是,您可以在方法中初始化指針,然后返回指針並在調用它的位置捕獲它。
您還假定數組的大小正確,如您自己的示例所示,這是不正確的。 您應該始終初始化指針。 至少將它們指向0,如下所示:
double *cff = NULL; // = 0 also works, but I like pointing pointers to NULL
該方法在以下方面已盡力解決:
double * Matrix::WriteToArray(){
double * CopyOfArray = NULL;
CopyOfArray = new double[n_rows*n_cols];
int i, j;
for(i=0;i<n_rows;i++){
for(j=0;j<n_cols;j++){
CopyOfArray[i*n_rows+j]=array[i*n_rows+j];
i++;
}
}
return CopyOfArray;
}
然后這樣稱呼它:
double *cff = NULL;
cff = a.WriteToArray();
警告:如果您在不存儲返回值的情況下調用該方法,則會泄漏內存。 不要使用指針,了解智能指針。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.