简体   繁体   English

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

[英]C++: Copy elements of matrix class to an array

I have this method (Matrix::WriteToArray(double &CopyOfArray)) that I want to write a copy of an array in the Matrix object to an array of doubles (ie CopyOfArray). 我有此方法(Matrix :: WriteToArray(double&CopyOfArray)),我想将Matrix对象中的数组副本写入double数组(即CopyOfArray)。 I am having trouble compiling though. 我在编译时遇到了麻烦。

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

Error: 错误:

$ 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

Here are the supporting files: Main.cpp 以下是支持文件:Main.cpp

int mrstart(double hcen, double mr[],  Matrix &a,  Matrix &HT)
{
    double *cff;
    a.WriteToArray(&cff);
    /*...*/
}

Matrix.cc 矩阵

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

Matrix.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

You want 你要

int Matrix::WriteToArray(double CopyOfArray[], const int size){
    //make sure size >= n_rows then copy
}

and call it like so 并这样称呼它

double cff[MAX_SIZE] = {};
a.WriteToArray(cff);

You should really use std::vector and not worry about dynamic allocation. 您应该真正使用std :: vector而不用担心动态分配。

EDIT: ok you can do manual allocation if you really want to but be careful to release it: 编辑:好的,如果您确实愿意,可以进行手动分配,但是要小心释放它:

double* cff = 0;
a.WriteToArray(cff);
//do stuff with cff
delete [] cff;

And inside you write function 在你里面写功能

int Matrix::WriteToArray(double *dest){
 dest = new double[n_rows];
 //copy data into dest
}

The main thing is to make sure you delete dest when you are done using it in main so there is no memory leakage. 最主要的是确保在main中使用完dest后将其删除,以免造成内存泄漏。

double *cff;
a.WriteToArray(&cff);

You are declaring a pointer, then using it before initializing it. 您要声明一个指针,然后在初始化之前使用它。 You are passing the function a pointer that doesn't point to anything. 您正在向函数传递没有指向任何内容的指针。 You should either declare the array statically, if you know at compile time the size 如果在编译时知道大小,则应该静态声明数组

double cff[16]; // 4x4 array, for example
a.WriteToArray(cff);

or size it appropriately before calling the function. 或在调用函数之前适当调整大小。

double * cff = new double[n_rows * n_cols];
a.WriteToArray(cff);

A few other criticisms: your function expects a reference to a double as an argument. 其他一些批评:您的函数期望引用double作为参数。 If you want to receive an array, the usual way to do it is to request a pointer. 如果要接收一个数组,通常的方法是请求一个指针。 A better way is not to use them at all and use some manner of smart pointer. 更好的方法是根本不使用它们,而使用某种形式的智能指针。

The method itself is broken too. 该方法本身也被破坏。

CopyOfArray[i]=array[i*n_cols];
i++;

This will result in you writing the first element of each row in the array, and leaving one space empty between them. 这将导致您将数组中每一行的第一个元素写入,并在它们之间留出一个空格。

You need a nested loop. 您需要一个嵌套循环。 You also shouldn't be returning anything, you are already writing to the parameter array so the return value is redundant. 您也不应该返回任何内容,因为您已经在写入参数数组,因此返回值是多余的。 You should also never return a pointer as an int, you should return it as a pointer. 您也不应将指针作为int返回,而应将其作为指针返回。 Better yet, though, you can initialize the pointer in the method, then return the pointer and catch it where you called it. 不过,更好的是,您可以在方法中初始化指针,然后返回指针并在调用它的位置捕获它。

You also assume the array is the right size, which as your own example proved is not true. 您还假定数组的大小正确,如您自己的示例所示,这是不正确的。 You should ALWAYS initialize pointers. 您应该始终初始化指针。 Point them at 0 at the very least, like so: 至少将它们指向0,如下所示:

double *cff = NULL; // = 0 also works, but I like pointing pointers to NULL

The method, fixed as much as I could, below: 该方法在以下方面已尽力解决:

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;
}

Then call it like so: 然后这样称呼它:

double *cff = NULL;
cff = a.WriteToArray();

WARNING: If you call the method without storing the return value you WILL leak memory. 警告:如果您在不存储返回值的情况下调用该方法,则会泄漏内存。 Don't use pointers, learn about smart pointers. 不要使用指针,了解智能指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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