繁体   English   中英

C ++:将矩阵类的元素复制到数组

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM