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
1
2
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  
    Matrix(Index n1) : Matrix_base<T>(n1), d1(n1) { }

    Matrix(Row<T,1>& a) : Matrix_base<T>(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<T>(a.size(),0), d1(a.d1)
    {
        // std::cerr << "copy ctor\n";
        this->base_copy(a);
    }

    template<int n> 
    Matrix(const T (&a)[n]) : Matrix_base<T>(n), d1(n)
        // deduce "n" (and "T"), Matrix_base allocates T[n]
    {
        // std::cerr << "matrix ctor\n";
        for (Index i = 0; i<n; ++i) this->elem[i]=a[i];
    }

    Matrix(const T* p, Index n) : Matrix_base<T>(n), d1(n)
        // Matrix_base allocates T[n]
    {
        // std::cerr << "matrix ctor\n";
        for (Index i = 0; i<n; ++i) this->elem[i]=p[i];
    }
    template<class F> Matrix(const Matrix& a, F f) : Matrix_base<T>(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; i<this->sz; ++i) this->elem[i] = f(a.elem[i]); 
    }

    template<class F, class Arg> Matrix(const Matrix& a, F f, const Arg& t1) : Matrix_base<T>(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; i<this->sz; ++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.

1
2
int r = 4, c = 8;
Numeric_lib::Matrix<int,2> mi (r, c); 


1
2
int r = 4, c = 8;
Numeric_lib::Matrix<int,2> 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.
And since you did not show your header I can't tell.
Generally: It is not the best idea to put using namespace std especially in the header.
Here is the whole class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include "Matrix.h"
using namespace std;

class T {

public:
	T(int r, int c) : ro(r), co(c) { }
	Numeric_lib::Matrix<int, 2> 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<int, 2> mi(ro, co);

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


1
2
3
4
5
6
7
8
9
10
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<int, 2> mi;

private:
	int ro, co;
};

Last edited on
Thank you, it worked.

Please take a look at this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class T {

public:
	T(int r, int c) : ro(r), co(c), mi(r, c) { }
	Numeric_lib::Matrix<string, 2> 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.