C++: Copy elements of matrix class to an array
<p>I have this method (Matrix::WriteToArray(double &amp;CopyOfArray)) that I want to write a copy of an array in the Matrix object to an array of doubles (i.e. CopyOfArray).
I am having trouble compiling though.</p>

<p>Any help is appreciated.
Thanks</p>

<p>Error:</p>

<pre><code>$ make
g++ -g -Wall -c main.cpp
main.cpp: In function ‘int mrstart(double, double*, Matrix&amp;, Matrix&amp;)’:
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
</code></pre>

<p>Here are the supporting files:
Main.cpp</p>

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

<p>Matrix.cc</p>

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

<p>Matrix.hh</p>

<pre><code>#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 &amp;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 &amp;CopyOfArray);

};
#endif
</code></pre>
