J
jr.freester
I have a written a simple class called Matrix. It allows me to
operate on a 2D array if it were a matrix. I have not really written
a class in c++ since the single course I took in college. In the
class Matrix I have a function toMatrix that allows me to copy a
standard array to a Matrix object. I have written a driver program to
test out the class. When compiling with gcc 4.1.2 I get the following
error ' error: ‘toMatrix’ was not declared in this scope'. Below is a
snippet of my code.
matrix.cpp
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Matrix
{
private:
int row; //number of rows;
int col; //number of col;
double *data; //pointer to pointers (2D data structure of the
matrix
public:
/*Constructor*/
Matrix(const int& M, const int& N): row(M), col(N)
{
data = new double[row*col]; //allocate memory for array of
double of column
for(int m = 0; m<col; m++)
{
for(int n = 0; n<row; n++)
{
data[m*col + n] = 0;
}
}
}
/*Copy Constructor*/
Matrix(const Matrix& old_Matrix): row(old_Matrix.row),
col(old_Matrix.col)
{
data = new double[row*col];
for(int m = 0; m<row; m++)
{
for(int n = 0; n<col; n++)
{
data[m*col + n] = old_Matrix.data[m*col + n];
}
}
}
/*Destructor*/
~Matrix()
{
delete[] data;
data = NULL;
}
-------------> break ----------------------------->
/*Copies standard array to Class Matrix*/
Matrix toMatrix(const double A[], const int& M, const int& N)
{
Matrix C(this->row, this->col);
row = M;
col = N;
for(int m = 0; m<row; m++)
{
for(int n = 0; n<col; n++)
{
data[m*col + n] = A[m*col + n];
}
}
C.row = this->row;
C.col = this->col;
return C;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
driver.cpp
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "matrix.cpp"
using namespace std;
int main()
{
double a[2][2] = {{1,2},{3,4}};
double b[4] = {9, 10, 11, 12};
Matrix A(2,2);
Matrix B(2,2);
A = toMatrix(b, 2, 2);
return 0;
}
operate on a 2D array if it were a matrix. I have not really written
a class in c++ since the single course I took in college. In the
class Matrix I have a function toMatrix that allows me to copy a
standard array to a Matrix object. I have written a driver program to
test out the class. When compiling with gcc 4.1.2 I get the following
error ' error: ‘toMatrix’ was not declared in this scope'. Below is a
snippet of my code.
matrix.cpp
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Matrix
{
private:
int row; //number of rows;
int col; //number of col;
double *data; //pointer to pointers (2D data structure of the
matrix
public:
/*Constructor*/
Matrix(const int& M, const int& N): row(M), col(N)
{
data = new double[row*col]; //allocate memory for array of
double of column
for(int m = 0; m<col; m++)
{
for(int n = 0; n<row; n++)
{
data[m*col + n] = 0;
}
}
}
/*Copy Constructor*/
Matrix(const Matrix& old_Matrix): row(old_Matrix.row),
col(old_Matrix.col)
{
data = new double[row*col];
for(int m = 0; m<row; m++)
{
for(int n = 0; n<col; n++)
{
data[m*col + n] = old_Matrix.data[m*col + n];
}
}
}
/*Destructor*/
~Matrix()
{
delete[] data;
data = NULL;
}
-------------> break ----------------------------->
/*Copies standard array to Class Matrix*/
Matrix toMatrix(const double A[], const int& M, const int& N)
{
Matrix C(this->row, this->col);
row = M;
col = N;
for(int m = 0; m<row; m++)
{
for(int n = 0; n<col; n++)
{
data[m*col + n] = A[m*col + n];
}
}
C.row = this->row;
C.col = this->col;
return C;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
driver.cpp
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include "matrix.cpp"
using namespace std;
int main()
{
double a[2][2] = {{1,2},{3,4}};
double b[4] = {9, 10, 11, 12};
Matrix A(2,2);
Matrix B(2,2);
A = toMatrix(b, 2, 2);
return 0;
}