Writing as separate header and main file

Hello,
I am trying to learn C++ on my own.I came across this thread http://www.cplusplus.com/forum/beginner/3473/ and tried my hands on some of the problems.I was able to write initial code for ques no 25 there(i.e,Write a program using class to perform addition,subtraction and multiplication of matrices) and it compiles correctly.But when I tried to write the same code using header file matrix.h and matrix.cpp(as in Visual C++ if you add a class),I am having difficulties.

Can somebody please point out the errors(and possibly correction,even more,explanation :-)) in the code,I shall be thankful.

matrix.h file:
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
#ifndef MATRIX_H
#def MATRIX_H
#pragma once
#include <iostream>

class matrix
{

    int nRow,nColumn;
 
    int **mat;
public:

    matrix(int nX,int nY);
 
    int returnRow(){return nRow;}
 
    int returnColumn(){return nColumn;}
 
    void input();
 
    void out();
 
    matrix operator+(matrix X);
 
    matrix operator-(matrix X);
 
    matrix operator*(matrix X);
 
};
#include "matrix.cpp"
#endif 


matrix.cpp file:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
   #include<iostream>
#include "matrix.h"
using namespace std;

matrix::matrix(int nX,int nY)
{
  nRow=nX;
  nColumn=nY;
  mat=new int*[nRow];                                
  for(int i=0;i<nRow;i++)
	  mat[i]=new int[nColumn];
  for(int i=0;i<nRow;i++)
   for(int j=0;j<nColumn;j++)
    mat[i][j]=0;
 }

void matrix::input()
{
	cout<<"Enter the elements of matrix: ";
  for(int i=0;i<nRow;i++)
   for(int j=0;j<nColumn;j++)
    cin>>mat[i][j];
}

void matrix::out()
{
  for(int i=0;i<nRow;i++)
   {
    cout<<"\n";
    for(int j=0;j<nColumn;j++)
	cout<<mat[i][j]<<" ";
   };
 }

matrix matrix::operator+(matrix X)
	{
  matrix C(nRow,nColumn);
  if(nRow==X.returnRow() && nColumn==X.returnColumn())
   {
    for(int i=0;i<nRow;i++)
      for(int j=0;j<nColumn;j++)
	   C.mat[i][j]=mat[i][j]+X.mat[i][j];
	return C;
   }
  else
  {
   cout<<"Matrix cant be added as dimensions are not the same";
   return C;
  }
 }

matrix matrix::operator-(matrix X)
	{
  matrix C(nRow,nColumn);
  if(nRow==X.returnRow() && nColumn==X.returnColumn())
   {
    for(int i=0;i<nRow;i++)
      for(int j=0;j<nColumn;j++)
	   C.mat[i][j]=mat[i][j]-X.mat[i][j];
	return C;
   }
  else
  {
   cout<<"Matrix cant be subtracted as dimensions are not same";
   return C;
  }
 }

matrix matrix::operator*(matrix X)
	{
  matrix C(nRow,X.returnColumn());
  if(nColumn==X.returnRow())
   {
    for(int i=0;i<nRow;i++)
      for(int j=0;j<X.returnColumn();j++)
	     for(int k=0;k<nColumn;k++)
	       C.mat[i][j]+=mat[i][k]*X.mat[k][j];
	return C;
   }
  else
  {
   cout<<"Matrix cant be multiplied as dimensions are not compatible";
   return C;
  }
 }


and my main.cpp file is:
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
#include <iostream>
#include "matrix.h"

using namespace std;

int main()
{
 int nX,nY;
 cout<<"Enter the dimension of first matrix,i.e,A:\n No. of Rows :";
 cin>>nX;
 cout<<"\n No of Columns :";
 cin>>nY;
 matrix A(nX,nY);
 A.input();
 cout<<"\nEnter the dimension of second matrix,i.e,B:\n No of Rows :";
 cin>>nX;
 cout<<"\nNo of Columns :";
 cin>>nY;
 matrix B(nX,nY);
 B.input();
 cout<<"Choose what opeartions would you like to perform:\n1.Add:   Gives A+B\n2.Subtract  Gives A-B\n3.Multiply  Gives A*B\n";
 int nOption;
 cin>>nOption;
 if(nOption==1)
  (A+B).out();
 else if(nOption==2)
  (A-B).out();
 else if(nOption==3)
  (A*B).out();
 else
  cout<<"Wrong choice,Please enter 1,2 or 3"<<endl;
 return 0;
}



It shows a variety of errors while compiling in Visual C++ express.

the original code is

Matric.cpp:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>

using namespace std;
class Matrix
{
 int nRow,nColumn;
 
 int **mat;                                            //double pointer
 public:
 Matrix(int nX,int nY)
 {
  nRow=nX;
  nColumn=nY;
  mat=new int*[nRow];                                
  for(int i=0;i<nRow;i++)
	  mat[i]=new int[nColumn];
  for(int i=0;i<nRow;i++)
   for(int j=0;j<nColumn;j++)
    mat[i][j]=0;
 }
 int returnRow()
 {
  return nRow;
 }
 int returnColumn()
 {
  return nColumn;
 }
 void input()
 {
	

  cout<<"Enter the elements of matrix: ";
  for(int i=0;i<nRow;i++)
   for(int j=0;j<nColumn;j++)
    cin>>mat[i][j];
 
 }
 void out()
 {
  for(int i=0;i<nRow;i++)
   {
    cout<<"\n";
    for(int j=0;j<nColumn;j++)
	cout<<mat[i][j]<<" ";
   }
 }
 Matrix operator+(Matrix X)//Opearator overload
 {
  Matrix C(nRow,nColumn);
  if(nRow==X.returnRow() && nColumn==X.returnColumn())
   {
    for(int i=0;i<nRow;i++)
      for(int j=0;j<nColumn;j++)
	   C.mat[i][j]=mat[i][j]+X.mat[i][j];
	return C;
   }
  else
  {
   cout<<"Matrix cant be added as dimensions are not the same";
   return C;
  }
 }
 Matrix operator-(Matrix X)//operator overload
 {
  Matrix C(nRow,nColumn);
  if(nRow==X.returnRow() && nColumn==X.returnColumn())
   {
    for(int i=0;i<nRow;i++)
      for(int j=0;j<nColumn;j++)
	   C.mat[i][j]=mat[i][j]-X.mat[i][j];
	return C;
   }
  else
  {
   cout<<"Matrix cant be subtracted as dimensions are not same";
   return C;
  }
 }
 Matrix operator*(Matrix X)//operator overloading
 {
  Matrix C(nRow,X.returnColumn());
  if(nColumn==X.returnRow())
   {
    for(int i=0;i<nRow;i++)
      for(int j=0;j<X.returnColumn();j++)
	     for(int k=0;k<nColumn;k++)
	       C.mat[i][j]+=mat[i][k]*X.mat[k][j];
	return C;
   }
  else
  {
   cout<<"Matrix cant be multiplied as dimensions are not compatible";
   return C;
  }
 }
};
int main()
{
 int nX,nY;
 cout<<"Enter the dimension of first matrix,i.e,A:\n No. of Rows :";
 cin>>nX;
 cout<<"\n No of Columns :";
 cin>>nY;
 Matrix A(nX,nY);
 A.input();
 cout<<"\nEnter the dimension of second matrix,i.e,B:\n No of Rows :";
 cin>>nX;
 cout<<"\nNo of Columns :";
 cin>>nY;
 Matrix B(nX,nY);
 B.input();
 cout<<"Choose what opeartions would you like to perform:\n1.Add:   Gives A+B\n2.Subtract  Gives A-B\n3.Multiply  Gives A*B\n";
 int nOption;
 cin>>nOption;
 if(nOption==1)
  (A+B).out();
 else if(nOption==2)
  (A-B).out();
 else if(nOption==3)
  (A*B).out();
 else
  cout<<"Wrong choice"<<endl;
return 0;
}



Thanks for reading.


P.S:In the single file,Matrix is used,whereas in others matrix is used as Visual C++ was not allowing Matrix as class name.
Last edited on
closed account (zb0S216C)
What exactly is wrong with it? Does it print funky numbers? Are you receiving access violations or heap corruptions? You need to be more specific so we can isolate the problem (s).

Note that operators should return references to *this, not copies of local structure instantiations.

Honestly, I'm not reading your code because I haven't the foggiest. I'm not being aggressive or anything, I'm just saying that programmers want to know what they're looking for.

Wazzak
Last edited on
Thanks for the reply

the code presented in Matrix.cpp (last code) was written by me originally.It compiles and produces desired output.

But when I tried to write it using separate header file and main file,it failed to compile.I think the problem is in "dividing" the single file in separate header files,etc as I am not proficient with that.

Some of the problems:
In line 35 of matrix.cpp(and similar lines for operator overload).I think it is wrong,but don't know right way to write.

Also,the variables defined in matrix.h,eg nRow,nColumn and mat are not being recognised.

Thanks

If anyone wants,i can copy the error log produced,but that would make this post less readable.


Lastly,I agree to the fact that my post is messy to look,and I apologise for that.But as I am new here,so am unaware if "Hide" can be used(in some forums,something like 'spoiler" or 'hide" is there)??
Last edited on
closed account (zb0S216C)
At a quick glance, I've noticed new was called, but delete wasn't. In short, you have multiple memory leaks. In addition, your matrix class doesn't have a destructor (well, it does, but in this case, not the want one you want). If a data member of class dynamically allocates a region of memory, the default destructor should be overloaded and should release that region of memory with the correct call to delete.

Also, the + operator should not actually modify the members of either operand. Instead, use the bitwise addition operator (+=). This should be the same with all operators.

Another important issue is with your header guards. The problem is that you have none. Your header should look like this:

1
2
3
4
5
#ifndef HEADER_H
#define HEADER_H
// Or optionally: #if !defined( HEADER_H )

#endif 

Placing class/function definitions within a separate source module is encouraged.

I'll keep scanning.

I've noticed that you added: i can copy the error log produced (sic). Yes please but could you use this link instead: http://pastebin.com/ :)

Wazzak
Last edited on
thanks for the info :-)

I've copy pasted the errors.

http://pastebin.com/AWaRXrae

thanks


P.S:I've edited the header file as you asked(i suppose it was to avoid more than one calling of same header?)

Also I think the destructor should be like this:

1
2
3
4
5
6
matrix :: ~matrix()
{
    for(int i=0;i<nRow;i++)
    delete mat[i];
    delete mat;
}


Any comment??Is it right?
Last edited on
I recently caught a major error in the matrix.cpp file and I've edited it.

1
2
3
4
5
6
//Initial statement
matrix matrix::matrix operator-(matrix X) //was wrong

//I replaced it by
matrix matrix::operator+(matrix X)


now the number of errors reduced a lot.

the new log is:http://pastebin.com/C7jDQqu6

Also i could not understand your comment about + operator.Can you please elaborate??
Don't include "matrix.cpp" in matrix.h.
That should reduce the number of errors. about redeceleration...
Thanks for your help.
It now compiles. :-)

Also can anybody help me with my bad practices in programming(some comments,like Framework gave).

P.S:I am marking it as solved.

Thanks to Framework and Nisheeth.
I can't say any thing as bad practice in the above code (on the basis of what I know, which is little itself!), but you should have a read of this:
http://www.cplusplus.com/articles/G8hv0pDG/
Last edited on
Topic archived. No new replies allowed.