473,769 Members | 5,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ - Errors met while designing a Matrix class

34 New Member
Hi guys,
well i am learning c++ on my own..
i am studying it by refering to the book and doin its exercises.
well i need help sorting out the errors.. and i am not sure wat destructors and constructors are.. plus i am coding using Visual C++ 2008 express edition..
this program needs me to input a matrix from a file..
the file called matrix1.txt is as follows:
<matrix>
rows = 2
cols = 2

1 2
2 4
</matrix>
so we are meant to input the data from the file into the matrix and carry out the operations..

the code is as follows for Matrix.h
Jul 31 '08 #1
8 1970
slizorn
34 New Member
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2.  
  3. class Matrix
  4. {
  5. public:
  6.     Matrix(void);
  7.     ~Matrix(void);
  8.     void readMatrixFile(string FILENAME);
  9.     void addMatrix(Matrix m1, Matrix m3);
  10.     void displayMatrix();
  11.     void transposeMatrix();
  12.     void multiplyMatrices(Matrix m2, Matrix m3);
  13.  
  14. private:
  15.     int row;
  16.     int col;
  17.     double m[][];
  18. };
  19.  
  20.  

the code for Matrix.cpp is as follows

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "Matrix.h"
  5. using namespace std;
  6.  
  7.  
  8. Matrix::Matrix(void)
  9. {
  10. }
  11.  
  12.  
  13. Matrix::~Matrix(void)
  14. {
  15. }
  16.  
  17.  
  18. Matrix::readMatrixFile(string FILENAME)
  19. {
  20.     const string filename(FILENAME);
  21.     ifstream file(filename.c_str());
  22.     string line;
  23.     if (getline(file, line) && line == "<matrix>")
  24.     {
  25.         if (file >> line /* "rows" */ && file >> line /* "=" */ && file >> row && getline(file,line) /* ; */)
  26.         {
  27.             if (file >> line /* "cols" */ && file >> line /* "=" */ && file >> col && getline(file,line) /* ; */)
  28.             {
  29.                 for (int r = 0; r < row; r++)
  30.                 {
  31.                     for (int c = 0; c < col; c++)
  32.                     {
  33.                         int value;
  34.                         if (file >> value)
  35.                         {
  36.                             m[r][c] = value;
  37.                         }
  38.                     }
  39.                     cout << endl;
  40.                 }
  41.             }
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47. Matrix::addMatrix(Matrix m1, Matrix m3) // needs some modification...
  48. {
  49.     for(int a = 0; a < row; a++)
  50.     {
  51.         for(int b = 0; b < col; b++)
  52.         {
  53.             m3[a][b]=(m1[a][b] + m2[a][b]);
  54.         }
  55.     }
  56. }
  57.  
  58.  
  59. Matrix::displayMatrix()
  60. {
  61.     for(int i = 0; i < row; i++)
  62.     {
  63.         cout << "| ";
  64.         for (int j = 0; j < col; j++)
  65.         {
  66.             cout << m3[i][j] << " ";
  67.         }
  68.         cout << "|" << endl;
  69.     }
  70. }
  71.  
  72.  
  73. Matrix::transposeMatrix()
  74. {
  75.     for(int k = 0; k < row; k++)
  76.     {
  77.         for(int j = 0; j < col; j++)
  78.         {
  79.             int save[2][2];
  80.             save[k][j] = m1[k][j];
  81.             m3[k][j] = m1[j][k];
  82.             m3[j][k] = save[k][j];
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. Matrix::multiplyMatrices(Matrix m2, Matrix m3) // needs soem modification like addMatrix
  89. {
  90.     if(m1.col != m2.row)
  91.     {
  92.         cout << "These two matrices cannot be multiplied!!!" << endl;
  93.     }
  94.     else
  95.     {
  96.         for(int i = 0; i < m1.row; i++)
  97.         {
  98.             for(int j = 0; j < m2.col; j++)
  99.             {
  100.                 for(int k = 0; k < m2.row; k++)
  101.                 {
  102.                     m3[i][j] = m3[i][j] + m1[i][k] * m2[k][j];
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }
  108.  
and the code for exercise2.cpp is as follows
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "Matrix.h"
  5. using namespace std;
  6.  
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     Matrix m1, m2 ,m3;
  11.     char theOption;
  12.     string filename1, filename2;
  13.  
  14.     while(theOption != '5')
  15.     {
  16.         cout << "Please choose one of the following options from the menu below:" << endl;
  17.         cout << "---------------------------------------------------------------" << endl;
  18.         cout << "(1) Add two matrices" << endl;
  19.         cout << "(2) Multiply two matrices" << endl;
  20.         cout << "(3) Take transpose of a matrix" << endl;
  21.         cout << "(4) Display a matrix" << endl;
  22.         cout << "(5) Exit" << endl;
  23.         cout << "---------------------------------------------------------------" << endl;
  24.         cin >> theOption;
  25.  
  26.         switch(theOption)
  27.         {
  28.             case '1':
  29.                 cout << "You have chosen option 1!: Add two matices!" << endl;
  30.                 cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
  31.                 cout << "Enter name of 1st file" << endl;
  32.                 cin >> filename1;
  33.                 cout << "Enter name of 2nd file" << endl;
  34.                 cin >> filename2;
  35.                 m1.readMatrixFile(filename1);
  36.                 m2.readMatrixFile(filename2);
  37.                 m2.addMatrix(m1, m3);
  38.                 cout << "The two matrices have been added together." << endl;
  39.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  40.                 break;
  41.  
  42.             case '2':
  43.                 cout << "You have chosen option 2!: Multiply two matices!" << endl;
  44.                 cout << "Please enter the two filenames that contain the two input matrices respectively!" << endl;
  45.                 cout << "Enter name of 1st file" << endl;
  46.                 cin >> filename1;
  47.                 cout << "Enter name of 2nd file" << endl;
  48.                 cin >> filename2;
  49.                 m1.readMatrixFile(filename1);
  50.                 m2.readMatrixFile(filename2);
  51.                 m1.multiplyMatrices(m2, m3);
  52.                 cout << "The two matrices have been multiplied with each other." << endl;
  53.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  54.                 break;
  55.  
  56.             case '3':
  57.                 cout << "You have chosen option 3!: Transpose a matrix!" << endl;
  58.                 cout << "Please enter the filename that contains the matrix to be transposed!" << endl;
  59.                 cout << "Enter name of file" << endl;
  60.                 cin >> filename1;
  61.                 m1.readMatrixFile(filename1);
  62.                 m1.transposeMatrix();
  63.                 cout << "The matrix has been transposed." << endl;
  64.                 cout << "Select (4): Display a matrix to view it." << endl << endl;
  65.                 break;
  66.  
  67.             case '4':
  68.                 cout << "You have chosen option 4!: Display a matix!" << endl;
  69.                 cout << "Please enter the option 1, 2 or 3 to display the respective matrices!" << endl;
  70.                 cin >> whichMatrixToDisplay;
  71.                 displayMatrix();
  72.                 cout << endl;
  73.                 break;
  74.  
  75.             case '5':
  76.                 cout << "You have chosen the option to Quit. Goodbye!" << endl;
  77.                 break;
  78.  
  79.             default:
  80.                 cout << " Sorry the option chosen is invalid. Please enter a valid option!" << endl << endl;
  81.                 break;
  82.         }
  83.     }
  84.  
  85.     return 0;
  86. }
  87.  
what is wrong with my program?
thanks in advance.. :D
Jul 31 '08 #2
arnaudk
424 Contributor
Why do you ask? What happened when you tried to compile and run your code?
Please enclose your code in [i][code=cpp[/i]] ... [/code] tags.
Jul 31 '08 #3
slizorn
34 New Member
hi i got lots of errors.
i tried for a few hours to reduce them..
i managed to bring it down from abt 120 errors to 43 errors..
and i tried my best to correct each error..
but looks like i need more experience or help before i can fix them
could u please help me spot the errors?
and teach me how to use the destructos and constructors?
thanks
Jul 31 '08 #4
newb16
687 Contributor
hi i got lots of errors.
i tried for a few hours to reduce them..
i managed to bring it down from abt 120 errors to 43 errors..
and i tried my best to correct each error..
but looks like i need more experience or help before i can fix them
Is there anything more difficult than missed declaration?

just a few
1) Ewe never allocate memory for m[][], it will crash on first run.
2) in multiplymatrix there is no m1 variable in scope, and m3 elements are left unitialized before ewe add values to them.

eta-47: error: ISO C++ forbids declaration of `readMatrixFile ' with no
type
- so what? add void in front of as in class declaration

::transposeMatr ix()
int save[2][2];
totaly wrong - use double as temp and I eve don't understand how is it supposed to work
m3 is not defined as well

error: no match for 'operator[]' in 'm3[a]'
Yes. Use m3.m[a] instead - from class methods ewe can access private members of classes of your type.
Jul 31 '08 #5
slizorn
34 New Member
[quote=newb16][quote=slizorn]hi i got lots of errors.
i tried for a few hours to reduce them..
i managed to bring it down from abt 120 errors to 43 errors..
and i tried my best to correct each error..
but looks like i need more experience or help before i can fix them
Is there anything more difficult than missed declaration?

just a few
1) Ewe never allocate memory for m[][], it will crash on first run.
2) in multiplymatrix there is no m1 variable in scope, and m3 elements are left unitialized before ewe add values to them.
yea about the allocating memory for the m[][]... how do i do it?
i have a rough idea that i got to use new and delete or somethin like that..
but not sure how...
could ya gimme an example for the case where the max size of the matrix is say 10 by 10?
and the thing with (2).. i want to be able to do m1.multiplyMatr ix(m2, m3);
but i get wat u mean but how am i to do it by initialize the m3 elements?

thanks
Jul 31 '08 #6
newb16
687 Contributor
yea about the allocating memory for the m[][]... how do i do it?
like m=new (double*)[10]; for(i...) m[i]=new double[10];
and... for(i...) delete[] m[i]; delete[] m;

and the thing with (2).. i want to be able to do m1.multiplyMatr ix(m2, m3);
but i get wat u mean but how am i to do it by initialize the m3 elements?
thanks
You need to set each element of m3 to 0 before addition - i.e. before the innermost loop.
Jul 31 '08 #7
slizorn
34 New Member
like m=new (double*)[10]; for(i...) m[i]=new double[10];
and... for(i...) delete[] m[i]; delete[] m;


You need to set each element of m3 to 0 before addition - i.e. before the innermost loop.


errm when i did wat u said for the 1st thing.. i jusut get abt 60 more errors... i dun think that is solving the problem.. if u want i can send u the files.. i cant attach it to this forum for some reason..

thanks
Jul 31 '08 #8
newb16
687 Contributor
I do not sly neponyal nothing. gugltransleyt some hnyu extradite apologize. Send-a ... in profile button pm - sends nivapros.
Jul 31 '08 #9

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

Similar topics

1
1767
by: murali | last post by:
Hi, I get tollowing linking errors Main.cpp:30 undefined reference to Matrix<int>::Matrix(int,int) whats that '' and what might be the problem ?? line 30 of main: Matrix<int>::Matrix(array,int rows,int cols)
5
3806
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
13
16735
by: Charulatha Kalluri | last post by:
Hi, I'm implementing a Matrix class, as part of a project. This is the interface I've designed: class Matrix( )
3
2478
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going wrong? I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains: //**************************** //**************************** Compiler output starts *********** cd /home/red/tmp/testprogs/
15
13277
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of matricies at compile-time? Any help would be appreciated. Hopefully this code comes in useful for someone, let me know if you find it useful, or if you have suggestions on how to improve it. // Public Domain by Christopher Diggins, 2005 ...
5
2026
by: pmatos | last post by:
Hi all, I have implemented a matrix template to suit my needs and now I've implemented some methods for computing SVD decomposition of Matrix<double>. I have: template <class T> class Matrix { public: ....
10
1906
by: andrea | last post by:
I'm studying some graphs algorithm (minumum spanning tree, breath search first topological sort etc etc...) and to understand better the theory I'm implementing them with python... I made my own graph class, the constructor is simply this: class graph: "in forma di matrice e' una matrice normale, in forma di lista uso un dizionario" def __init__(self,nodes,edges,dir=False,weight=): # inizializzatore dell'oggetto, di default in forma di...
2
8565
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and numeric because this is a personal programming exercise for me in creating an operational class in pure python for some *basic* matrix operations). 1. Please take a look at the __init__ function and comment on the initialization of the list data...
7
1665
by: mohammaditraders | last post by:
Write a program which overloads a binary Minus (+) operator, The program will contain a class Matrix, This class will contain a private data member Array which store int values. The class will further contain a Default constructor, get() function which takes values for array from the user and also contain a Display function witch display the array on the screen, In main function create three objects Mat1, Mat2, Mat3 of this class, first...
0
9589
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
9423
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
10049
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
9996
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
9865
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
8872
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...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
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();...
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.