A
aaragon
Hello! I've been working on enhancing a simple Matrix class that I
found on Stroustrup's "The C++ Programming Language" and I have a
problem with ambiguous constructor calls that I don't know if I can
solve.
The idea is:
template <typename T = double>
class Matrix
{
typedef T ValueType;
valarray<ValueType>* v_; //!< Valarray object used to store the
matrix elements by rows
size_t m_; //!< Unsigned integer used to
store the number of rows
size_t n_; //!< Unsigned integer used to
store the number of columns
public:
// matrix constructors
Matrix() : m_(), n_(), v_(NULL) {}
explicit Matrix(size_t n, ValueType val = 0) : m_(n), n_(n),
v_(new valarray<ValueType>(val, n*n)) {}
Matrix(size_t m, size_t n, ValueType val = 0) : m_(m), n_(n),
v_(new valarray<ValueType>(val,m*n)){}
Matrix(const Matrix& prototype) {
// copy number of rows and columns from matrix A
m_ = prototype.m_;
n_ = prototype.n_;
// allocates memory for storing the matrix elements
v_ = new valarray<ValueType>(*(prototype.v_));
}
Matrix& operator=(const Matrix& prototype) {
// check to avoid self-copy
if(this != &prototype)
{
// deletes dynamically allocated memory
delete v_;
// copy number of rows and columns from matrix A
m_ = prototype.m_;
n_ = prototype.n_;
// allocates memory for storing the matrix elements
v_ = new valarray<ValueType>(*(prototype.v_));
}
return *this;
}
....
....
....
Now, as you can see the second and third constructor calls are
ambiguous. Is there a way to get around this? I would like to have
both constructors. Also, I've been thinking in the best way to have a
constructor for identity matrices. I thought that makeing a
constructor:
Matrix(size_t, string);
would make a good choice and then comparing the string agains
"identity" or something like this. But maybe there is a better way to
do it, probably with an enumerated type?
Thanks for your suggestions!
a²
found on Stroustrup's "The C++ Programming Language" and I have a
problem with ambiguous constructor calls that I don't know if I can
solve.
The idea is:
template <typename T = double>
class Matrix
{
typedef T ValueType;
valarray<ValueType>* v_; //!< Valarray object used to store the
matrix elements by rows
size_t m_; //!< Unsigned integer used to
store the number of rows
size_t n_; //!< Unsigned integer used to
store the number of columns
public:
// matrix constructors
Matrix() : m_(), n_(), v_(NULL) {}
explicit Matrix(size_t n, ValueType val = 0) : m_(n), n_(n),
v_(new valarray<ValueType>(val, n*n)) {}
Matrix(size_t m, size_t n, ValueType val = 0) : m_(m), n_(n),
v_(new valarray<ValueType>(val,m*n)){}
Matrix(const Matrix& prototype) {
// copy number of rows and columns from matrix A
m_ = prototype.m_;
n_ = prototype.n_;
// allocates memory for storing the matrix elements
v_ = new valarray<ValueType>(*(prototype.v_));
}
Matrix& operator=(const Matrix& prototype) {
// check to avoid self-copy
if(this != &prototype)
{
// deletes dynamically allocated memory
delete v_;
// copy number of rows and columns from matrix A
m_ = prototype.m_;
n_ = prototype.n_;
// allocates memory for storing the matrix elements
v_ = new valarray<ValueType>(*(prototype.v_));
}
return *this;
}
....
....
....
Now, as you can see the second and third constructor calls are
ambiguous. Is there a way to get around this? I would like to have
both constructors. Also, I've been thinking in the best way to have a
constructor for identity matrices. I thought that makeing a
constructor:
Matrix(size_t, string);
would make a good choice and then comparing the string agains
"identity" or something like this. But maybe there is a better way to
do it, probably with an enumerated type?
Thanks for your suggestions!
a²