473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

=?windows-1252?Q?error=3A_=91toMatrix=92_was_not_declared_in _this_scope?=

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

Justin
Nov 2 '08 #1
2 1956
jr*********@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.

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
Nov 2 '08 #2
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 structureof 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
*/
Nov 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fox | last post by:
Hi, how can i convert a page from UTF-8 to Windows1252 encoding ? (to be displaying on German mobile that only accept WINDOWS1252 encoding)
12
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I...
4
by: lkrubner | last post by:
Pierre Goiffon Oct 6 2004, 4:29 am show options Newsgroups: comp.infosystems.www.authoring.html >> The problem with charset UTF-8 on pages with forms for e.g. >> guestbooks, formmail and...
9
by: musosdev | last post by:
I'm having trouble writing £ (pound sign) to a file from my asp.net app. I've tried.. StreamWriter.WriteLine("£") - and - StreamWriter.WriteLine(Convert.ToChar(156).ToString()) but neither...
14
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html...
66
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.