error: ‘toMatrix’ was not declared in this scope

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

Kai-Uwe Bux

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;

That line seems to have m and n the wrong way, or you want m<row, n<col.
}

}

}
/*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];
}
}
}

Here, you could just copy col*row elements linearly.
/*Destructor*/
~Matrix()
{
delete[] data;
data = NULL;

No need to set data=0.
}


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

This is written like a member function. From usage, that is not what you
want.

Also, it is terribly wrong, as it would totally mess up the current object
and return something entirely unrelated.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

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



Best

Kai-Uwe Bux
 
S

Salt_Peter

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::eek:stream&
operator<<( std::eek:stream& 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::eek:stream_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
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top