Matrices in C++

Hi,

I've included the Matrix header file (http://www.stroustrup.com/Programming/Matrix/Matrix.h) in my program, but the VS compiler still doesn't recognize the statement below!

`Matrix<int> mi;`
Last edited on
 ``12`` ``````private: Matrix(); // this should never be compiled ``````

Looks like the library doesn't want you to use the default constructor.

You need to use a different constructor for the Matrix object.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` `````` Matrix(Index n1) : Matrix_base(n1), d1(n1) { } Matrix(Row& a) : Matrix_base(a.dim1(),a.p), d1(a.dim1()) { // std::cerr << "construct 1D Matrix from Row\n"; } // copy constructor: let the base do the copy: Matrix(const Matrix& a) : Matrix_base(a.size(),0), d1(a.d1) { // std::cerr << "copy ctor\n"; this->base_copy(a); } template Matrix(const T (&a)[n]) : Matrix_base(n), d1(n) // deduce "n" (and "T"), Matrix_base allocates T[n] { // std::cerr << "matrix ctor\n"; for (Index i = 0; ielem[i]=a[i]; } Matrix(const T* p, Index n) : Matrix_base(n), d1(n) // Matrix_base allocates T[n] { // std::cerr << "matrix ctor\n"; for (Index i = 0; ielem[i]=p[i]; } template Matrix(const Matrix& a, F f) : Matrix_base(a.size()), d1(a.d1) // construct a new Matrix with element's that are functions of a's elements: // does not modify a unless f has been specifically programmed to modify its argument // T f(const T&) would be a typical type for f { for (Index i = 0; isz; ++i) this->elem[i] = f(a.elem[i]); } template Matrix(const Matrix& a, F f, const Arg& t1) : Matrix_base(a.size()), d1(a.d1) // construct a new Matrix with element's that are functions of a's elements: // does not modify a unless f has been specifically programmed to modify its argument // T f(const T&, const Arg&) would be a typical type for f { for (Index i = 0; isz; ++i) this->elem[i] = f(a.elem[i],t1); }``````
Actually you need the namespace Numeric_lib:

`Numeric_lib::Matrix<int> mi;`
@coder, thank you.

Last edited on
When I declare the following matrix in either way below in a class, and want to access it outside the class, the error says:

"mi" is not a member of class.

 ``12`` ``````int r = 4, c = 8; Numeric_lib::Matrix mi (r, c); ``````

 ``12`` ``````int r = 4, c = 8; Numeric_lib::Matrix mi {r, c};``````

While `mi` "is" a public member of the class!

One more question:
Does that matrix header file have collisions with `using namespace std` ?
Last edited on
To determine the error you need to show your class. Also the entire error.

 Does that matrix header file have collisions with using namespace std ?
The 'stroustrup' header does not contain that using.
Generally: It is not the best idea to put `using namespace std` especially in the header.
Here is the whole class:

 ``12345678910111213141516171819202122232425`` ``````#include #include "Matrix.h" using namespace std; class T { public: T(int r, int c) : ro(r), co(c) { } Numeric_lib::Matrix mi(ro, co); private: int ro, co; }; int main() { int row = 8, column = 4; T t(row, column); cout << t.mi.size() << endl; return 0; } ``````

And here are the errors:

Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "ro" is undefined

Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "co" is undefined

Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier 'ro'

 `Numeric_lib::Matrix mi(ro, co);`

If you want line 9 to be a matrix object within the T class,
Then do something like this:

 ``12345678910`` ``````class T { public: T(int r, int c) : mi(r, c), ro(r), co(c) { } // assuming there's a 2-arg Matrix constructor Numeric_lib::Matrix mi; private: int ro, co; };``````

Last edited on
Thank you, it worked.

Please take a look at this one:

 ``123456789101112131415161718192021222324252627`` ``````class T { public: T(int r, int c) : ro(r), co(c), mi(r, c) { } Numeric_lib::Matrix mi; private: int ro, co; }; int main() { int row = 2, column = 4; T t(row, column); cout << t.mi.size() << endl; t.mi(0, 1) = "first"; for (int i = 0; i < t.mi.dim1(); i++) for (int j = 0; j < t.mi.dim2(); j++) cout << t.mi(i, j) << ", "; cout << endl; system("pause"); return 0; }``````

Is there a better way to traverse the Matix above please?

And another question is, how to know how many cells of the matrix are filled in and how many of them are still empty, please? For example, in the above example, only one cell is filled ("first") and the rest are empty. How to know that?
Last edited on
Topic archived. No new replies allowed.