473,466 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Error from a matrix class

2 New Member
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error.

Expand|Select|Wrap|Line Numbers
  1. include <iostream>
  2. using namespace std;
  3.  
  4. class matrix{
  5.  
  6. public:
  7.  
  8. matrix();
  9. matrix(int m,int n);
  10.  
  11. int getRow();
  12. int getCol();
  13.  
  14. double& operator()(int, int);
  15.  
  16. friend ostream& operator<<(ostream& os, matrix& m);
  17. matrix operator + (matrix&);
  18. matrix operator * (matrix&);
  19. matrix operator - (matrix&);
  20. matrix operator / (matrix&);
  21.  
  22. private:
  23. void init(int, int);
  24. int nrows,ncols;
  25. double *data;
  26. };
  27.  
  28. matrix::matrix(){
  29. init(1,1);
  30. }
  31.  
  32. matrix::matrix(int m, int n){
  33. init(m,n);
  34. }
  35.  
  36. void matrix::init(int m, int n){
  37. nrows=m;
  38. ncols=n;
  39. data= new double[m*n];
  40. for(int i=0; i<m*n; i++)
  41. data[i]=0;
  42. }
  43.  
  44. int matrix::getRow() { return nrows;}
  45.  
  46. int matrix::getCol() { return ncols;}
  47.  
  48. double& matrix::operator ()(int r, int c){
  49. if (r <0 || r> nrows){
  50. cout<<"Illegal row index";
  51. return data[0];
  52. }
  53. else if (c <0 || c > ncols){
  54. cout<<"Illegal Column Index:";
  55. return data[0];
  56. }
  57. else return data[r*ncols+c];
  58. }
  59.  
  60. ostream& operator<<(ostream& os, matrix &m){
  61. int mval=m.getRow();
  62. int nval=m.getCol();
  63. for(int i=0; i<mval; i++){
  64. for(int j=0; j < nval; j++)
  65. os<<m(i,j);
  66. os<<endl;
  67. }
  68. return os;
  69. }
  70.  
  71. matrix matrix::operator+(matrix& a){
  72. matrix sum(nrows, ncols);
  73. for (int i=0; i<nrows; i++)
  74. for(int j=0; j<ncols; j++)
  75. sum(i,j) = (i,j) + a(i,j);
  76. return sum;
  77.  
  78. matrix matrix::operator*(matrix& a){
  79. matrix product(nrows, ncols);
  80. for (int i=0; i<nrows; i++)
  81. for(int j=0; j<ncols; j++)
  82. product(i,j) = (i,j) * a(i,j);
  83. return product;
  84.  
  85. matrix matrix::operator-(matrix& a){
  86. matrix difference(nrows, ncols);
  87. for (int i=0; i<nrows; i++)
  88. for(int j=0; j<ncols; j++)
  89. difference(i,j) = (i,j) - a(i,j);
  90. return difference;
  91.  
  92.  
  93. matrix matrix::operator/(matrix& a){
  94. matrix divide(nrows, ncols);
  95. for (int i=0; i<nrows; i++)
  96. for(int j=0; j<ncols; j++)
  97. divide(i,j) = (i,j) / a(i,j);
  98. return divide;
  99.  
  100.  
  101. }
  102. int main(){
  103. matrix a(2,2);
  104. a(1,1)=5.0;
  105. a(0,0)=6.0;
  106. a(0,1)=7.0;
  107. a(1,0)=8.0;
  108. cout<<a(0,0)<<endl;
  109. cout<<a(1,1)<<endl;
  110. cout<<a<<endl;
  111.  
  112. matrix b(2,2);
  113. b(0,0) = 5.0;
  114. b(0,1) = 5.0;
  115. b(1,0) = 5.0;
  116. b(1,1) = 5.0;
  117. cout<<b<<endl;
  118.  
  119. matrix c,d,e,f;
  120.  
  121. c=a+b;
  122. d=a*b;
  123. e=a-b;
  124. f=a/b;
  125.  
  126. cout<<c<<endl;
  127. cout <<"\n\n";
  128. cout<<d<<endl;
  129.  
  130. cout<<e<<endl;
  131. cout <<"\n\n";
  132. cout<<f<<endl;
  133.  
  134.  
  135. system("pause");
  136. return 0;
  137. }
Jan 3 '10 #1
2 2647
weaknessforcats
9,208 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. double& operator()(int, int);
This won't work.

The rules of operator overloading require that ast least one argument be a user-defined type. Like a class or a struct.
Jan 3 '10 #2
newb16
687 Contributor
It's a member of the matrix class and compiles without errors. The reason of the error is missing '#' in #include directive and missing closing brace in operator+ implementation (and other operators as well)
Jan 3 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Count Dracula | last post by:
I narrowed down the source of the error. The stand-alone program listed below reproduces it. The compilation finishes but the warning is too serious to ignore: cl -GX prog.cpp Microsoft (R)...
4
by: Zenon | last post by:
Folks, I have been trying for a week but I cannot debug the following error: Error E2285 ex5.cpp 141: Could not find a match for 'matrix<complex<double>>::operator =(complex<double>)' in...
5
by: tuko | last post by:
The following snipet gives a linker error. I don't get it... template<class T> class tran { public: public: private: }; template<class T> class matrix {
3
by: Benedikt Weber | last post by:
I found the following compiler error with Microsoft Visual C++ .NET. I use different functions with return types determined by a Traits class. Function g() below works ok, but when I put the two...
9
by: eiji | last post by:
Hi folks, I have a linker - problem using a Matrix-template. Maybe someone can help me with that. Q: "using femath::Matrix" or "using femath::Matrix<double>"; Consider this:
1
by: atomik.fungus | last post by:
Hi, as many others im making my own matrix class, but the compiler is giving me a lot of errors related to the friend functions which overload >> and <<.I've looked around and no one seems to get...
6
by: sadegh | last post by:
Hi I have a problem with my program in VC++6 When I compile it, the following errors are listed. I spend a lot of time on the groups.google.com to find its reason, but none of comments could...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
2
by: jr.freester | last post by:
I have a written a simple class called Matrix. It allows me to operate on a 2D array if it were a matrix. I have not really written a class in c++ since the single course I took in college. In...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.