0

我有这个方法(Matrix::WriteToArray(double &CopyOfArray)),我想将Matrix 对象中的一个数组的副本写入一个双精度数组(即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);
    /*...*/
}

矩阵.cc

int Matrix::WriteToArray(double &CopyOfArray){
    int i;
    for(i=0;i<n_rows;i++){
        CopyOfArray[i]=array[i*n_cols];
        i++;
    }
    return *CopyOfArray;
}

矩阵.hh

#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
4

2 回答 2

0

你要

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 后删除它,这样就不会出现内存泄漏。

于 2012-12-18T00:25:34.093 回答
0
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();

警告:如果您在不存储返回值的情况下调用该方法,您将泄漏内存。不要使用指针,了解智能指针。

于 2012-12-18T00:49:57.843 回答