c++ class

I define a class for generating matrix and there is no bug there but when in main program I want to see the matrix output it shows me nothing, I am not attaching the class, because it's a long code, but I am attaching the main code and i want to know if there is any problem here or not.
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
#include<iostream>
#include<string>
#include <math.h>
#include "Matrix2D.h"
using namespace std;


// a simple function to print a matrix to stdout.
template<class T>
void PrintMatrix(Matrix2D<T> matrix)
{
	int nRows = matrix.GetNumRows();
	int nCols = matrix.GetNumCols();
	for (int row = 0; row < nRows; row++)
	{
		for (int col = 0; col < nCols; col++)
		{
			cout << matrix.GetElement(row, col) << " ";
		}
		cout << endl;
	}
}

int main()
{
	cout << "code to test Matrix2D" << endl;
	// create an instance of the Matrix2D class

	double simpleData[12] = { 1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0 };
	Matrix2D<double>testMatrix(3, 4, simpleData);
	PrintMatrix(testMatrix);
	return 0;	
}
 
Last edited on
First of all, that should not show "nothing", because there are the cout << " " and cout << endl. Granted, whitespace is harder to see.

If you don't get even the whitespace, then GetNumRows() and GetNumCols() return 0 or less.

If you do get whitespace, then GetElement() returns unprintable content.

The only other members of Matrix2D that you use are:
* Custom constructor Matrix2D(int,int,T*)
* Copy constructor
* Destructor

If your code has no bugs and the main code does not have bugs, then who does?


PS. You don't use anything from <string> or <math.h> here. Do not include what you don't need.
Besides, <math.h> is from C library. In C++ you would use the <cmath>.
My bad, sorry,it types the first cout...
But I don't get matrix
But I don't get matrix

Either matrix.GetNumRows() or matrix.GetNumCols() is returning 0.

After line 13, insert a debugging display of nRows and nCols.

L10 Pass matrix by const ref and not by value. This is currently doing a copy-by-value and unless you're got the copy constructor correct (and tested) then this could be the issue. In case, to avoid the overhead of the copy it should be passed by const ref.

Topic archived. No new replies allowed.