473,729 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ 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 2665
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
3621
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) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
4
2027
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 function main() Here is the relevant portion of my template class //***************************************************************************
5
1786
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
2133
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 declarations f1() and f2(), the compiler gets disturbed. The error message does not even reproduce the original code correctly. When the two declarations are switched, the problem goes away. Benedikt
9
1827
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
2134
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 the same error. Here is the code of the class template< class T > class Matrix { friend ostream &operator << <>( ostream &, const Matrix< T > & ); friend istream &operator >> <>( istream &, Matrix< T > & ); public: ...
6
121911
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 not help me. Does any body know what is the problem?. Thanks. OtherFunctions.obj : error LNK2001: unresolved external symbol "int
8
2338
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
1984
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 the class Matrix I have a function toMatrix that allows me to copy a standard array to a Matrix object. I have written a driver program to test out the class. When compiling with gcc 4.1.2 I get the following error ' error: ‘toMatrix’ was not...
0
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.