Salt_Peter
11/2/2008 7:01:00 AM
On Nov 1, 11:20 pm, jr.frees...@gmail.com wrote:
> 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.
>
It looks like toMatrix is a member function (although your code is
missing a critical class closing brace + semicolon), in which case the
following:
A = toMatrix(b, 2, 2);
looks for a function in the global namespace that fits that signature.
B = A.toMatrix(b, 2, 2);
should work but thats bizarre, doesn't make sense to use a member
function and NOT directly modify that object's own data.
> 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;}
>
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Justin
Instead of using pointers to allocated arrays, prefer using containers
like std::vector. They take a little time to get to know them but
bring many benefits and advantages.
In this case, std::vector< std::vector< double > > will do and the
member function void assign(...) is overloaded.
#include <iostream>
#include <ostream>
#include <vector>
#include <algorithm>
#include <iterator>
template< typename T, const std::size_t row, const std::size_t col >
class Matrix
{
typedef std::vector< T > Vt;
std::vector< Vt > matrix;
// ctors
public:
Matrix()
: matrix(row, std::vector< T >(col)) { }
Matrix(const T& t)
: matrix(row, std::vector< T >(col, t)) { }
Matrix(const Matrix& copy)
: matrix(copy.matrix) { }
// member functions
void assign(T (& array)[row][col])
{
for(std::size_t r = 0; r < row; ++r)
{
for(std::size_t c = 0; c < col; ++c)
matrix[r][c] = array[r][c];
}
}
void assign(T (& array)[row * col])
{
std::size_t i(0);
for(std::size_t r = 0; r < row; ++r)
{
for(std::size_t c = 0; c < col; ++c)
matrix[r][c] = array[i++];
}
}
// friend op<<
friend std::ostream&
operator<<( std::ostream& os,
const Matrix< T, row, col>& mx )
{
typedef typename std::vector< Vt >::const_iterator VIter;
for( VIter it = mx.matrix.begin();
it != mx.matrix.end();
++it )
{
std::copy( (*it).begin(),
(*it).end(),
std::ostream_iterator< T >(os,"\t") );
os << std::endl;
}
return os;
}
};
int main()
{
Matrix< double, 4, 4 > m4x4(1.1);
std::cout << m4x4 << std::endl;
double arr[][4] = { {1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
m4x4.assign(arr);
std::cout << m4x4 << std::endl;
double a[2][2] = {{1,2},{3,4}};
// instance.assign(a); // error, no match
Matrix< double, 2, 2 > m2x2;
m2x2.assign(a);
std::cout << m2x2 << std::endl;
double a4[] = {1.1, 2.2, 3.3, 4.4};
m2x2.assign(a4);
std::cout << m2x2 << std::endl;
std::cout << "Press ENTER to EXIT.\n";
std::cin.get();
}
/*
1.1 1.1 1.1 1.1
1.1 1.1 1.1 1.1
1.1 1.1 1.1 1.1
1.1 1.1 1.1 1.1
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
1 2
3 4
1.1 2.2
3.3 4.4
*/