Function calls to dynamically allocated matrices
I'm having difficulty calling a function that manipulates a
dynamically allocated array and returns it to main. I don't
understand enought about how this class template works to successfully
do this, I suppose. If anyone has advice I'd appreciate it...
Dynamically allocated arrays template:
Cline, M., C++ FAQ Lite document, http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.19,
10/22/2008
template<class T> class Matrix {
public:
Matrix(unsigned nrows, unsigned ncols);
class BadSize { };
T& operator() (unsigned i, unsigned j);
const T& operator() (unsigned i, unsigned j) const;
class BoundsViolation { };
unsigned nrows() const;
unsigned ncols() const;
private:
std::vector<std::vector<T> > data_;
};
template<class T>
inline unsigned Matrix<T>::nrows() const
{ return data_.size(); }
template<class T>
inline unsigned Matrix<T>::ncols() const
{ return data_[0].size(); }
template<class T>
inline T& Matrix<T>::operator() (unsigned row, unsigned col)
{
if (row >= nrows() || col >= ncols()) throw BoundsViolation();
return data_[row][col];
}
template<class T>
inline const T& Matrix<T>::operator() (unsigned row, unsigned col)
const
{
if (row >= nrows() || col >= ncols()) throw BoundsViolation();
return data_[row][col];
}
template<class T>
Matrix<T>::Matrix(unsigned nrows, unsigned ncols)
: data_ (nrows)
{
if (nrows == 0 || ncols == 0)
throw BadSize();
for (unsigned i = 0; i < nrows; ++i)
data_[i].resize(ncols);
}
Within main, there is no difficulty manipulating array A. I'm having
problems setting up a function prototype and call that allow A to be
manipulated within the function and returned to main as A.
Function prototype:
int* FORM(int A(int x, int y));
{ Note: this declares the parameter A as a function type. -mod/sk }
Create array A:
Matrix<int>A(x, y);
Function call within main:
A = FORM( A(x, y) ); ERROR: error C2664: 'FORM' : cannot convert
parameter 1 from 'int' to 'int (__cdecl *)(int,int)'
Function:
int* FORM(int A(int x, int y));
// Do something with A
return A;
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]