By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

i write code but dont understand the error please help me

P: n/a
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
cout<<"paratmerized constructor called";
}
Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();

};
Matrix :: Matrix ( int rows = 0 , int cols = 0)
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;
}
}
}
void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";
}

}
//cout<<"Enter the 2nd Matrix"<<endl;
}
void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;
}
cout << "|" << endl ;
}

cout<<'\n';
cout<<'\n';
}

Matrix :: Matrix operator + (Matrix add)
{
Matrix temp;
temp.numCols=numCols + add.numCols;
temp.numRows=numRows + add.numRows;
return temp;
}

void main ( )
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatrix ( ) ;
matrix2.getMatrix();
//Matrix = matrix1 + matrix2 ;
matrix1.displayMatrix ( ) ;
matrix2.displayMatrix ( ) ;
//
system ( "PAUSE" ) ;
getch();
}

Jun 23 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Jun 22, 7:59 pm, mohammaditrad...@gmail.com wrote:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;

public :
Matrix(){
cout<<"paratmerized constructor called";}

Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();

};

Matrix :: Matrix ( int rows = 0 , int cols = 0)
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;}
}
}

void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";

}
}

//cout<<"Enter the 2nd Matrix"<<endl;}

void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )

{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;}

cout << "|" << endl ;

}

cout<<'\n';
cout<<'\n';

}

Matrix :: Matrix operator + (Matrix add)
{
Matrix temp;
temp.numCols=numCols + add.numCols;
temp.numRows=numRows + add.numRows;
return temp;

}

void main ( )
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatrix ( ) ;
matrix2.getMatrix();
//Matrix = matrix1 + matrix2 ;
matrix1.displayMatrix ( ) ;
matrix2.displayMatrix ( ) ;
//

system ( "PAUSE" ) ;
getch();

}- Hide quoted text -

- Show quoted text -

compile time error? run-time error? what is the program supposed to
do? what am i supposed to be looking for?

Jun 23 '07 #2

P: n/a
Zachary Turner wrote:

[snip scores of lines of code]
>

compile time error? run-time error? what is the program supposed to
do? what am i supposed to be looking for?
Looks like a trim-time error to me!

--
Ian Collins.
Jun 23 '07 #3

P: n/a

<mo**************@gmail.comwrote in message...

My suggestions inline below:

// #include <iostream.h>
// #include <stdlib.h>
// #include <conio.h>
// #include <string.h>

#include <iostream>
#include <cstdlib>
#include <string>
class Matrix{ private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
// cout<<"paratmerized constructor called";
std::cout<<"paratmerized constructor called";
}
Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();
};
// Matrix :: Matrix ( int rows = 0 , int cols = 0){
// numCols = cols ; // numRows = rows ;

Matrix :: Matrix ( int rows = 0 , int cols = 0) : // note that colon
numRows( rows ), numCols( cols ){

for ( int i = 0 ; i < numRows ; ++i ){
for ( int j = 0 ; j < numCols ; ++j ){
elements [ i ] [ j ] = 0 ;
}
}
}
void Matrix :: getMatrix ( ){
for ( int i = 0 ; i < numRows ; ++i ){
for ( int j = 0 ; j < numCols ; ++j ){
// cin >elements [ i ] [ j ] ;
std::cin >elements [ i ] [ j ] ;
file://cout<<"Enter the 1st Matrix";
}
}
file://cout<<"Enter the 2nd Matrix"<<endl;
}
void Matrix :: displayMatrix ( ){
for ( int i = 0 ; i < numRows ; ++i ){
// cout << "| " ;
std::cout << "| " ;
for ( int j = 0 ; j < numCols ; ++j ){
// cout << elements [ i ] [ j ] << " " ;
std::cout << elements [ i ] [ j ] << " " ;
}
// cout << "|" << endl ;
std::cout << "|" << endl ;
}
// cout<<'\n'; // cout<<'\n';
std::cout<<"\n"<<std::endl;
}

Matrix :: Matrix operator + (Matrix add){
Matrix temp;
temp.numCols=numCols + add.numCols;
temp.numRows=numRows + add.numRows;
return temp;
}

// void main ( )
int main ( ){
Matrix matrix1(2, 2), matrix2(2,2) ;
matrix1.getMatrix ( ) ;
matrix2.getMatrix();
file://Matrix = matrix1 + matrix2 ;
matrix1.displayMatrix ( ) ;
matrix2.displayMatrix ( ) ;
// system ( "PAUSE" ) ;
// getch();

std::string dummy;
std::cin >dummy;
}
Your main problems were:
1] wrong headers
2] unqualified 'std::'
3] main() returns type 'int', **always**!!
4] it's ++x or x++ ( NO space between them)

Try the corrections above, and post again if you have problems. But, post
what you expect the program to do, what you get, and the first 3 errors you
get if it doesn't compile.

--
Bob R
POVrookie
Jun 23 '07 #4

P: n/a

BobR wrote in message...
>
Your main problems were:
4] it's ++x or x++ ( NO space between them)
Oops. 4 does not exist. Forget I said that.

--
Bob R
POVrookie
Jun 23 '07 #5

P: n/a
You've made a few mistakes but they are just the usual beginner
mistakes, see below for some corrections.

You should try to write less code at once. When you write too much code
you just get errors on top of errors and it gets really confusing (you
know this). When you write code, just write a few more lines then try to
compile and run. Don't try to write thirty lines of code at once until
you get to be an expert. Don't try and write more code, when you don't
understand what's wrong with the code you've written. This is really
important advice, more important than anything else I tell you below.

mo**************@gmail.com wrote:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
Replace the above with

#include <iostream>
#include <conio.h>
using namespace std;

There is no header file called <iostream.hin C++. Anyone who tells you
different is wrong. *Some* compilers have it but some don't. When I
compile your code with my compielr it says

Cannot open include file: 'iostream.h': No such file or directory

which is right. The correct name for this header file is <iostream>
without the '.h'. There are header file called <stdlib.hand <string.h>
but you are not using them so you might as well get rid of them.

Finally you need 'using namespace std;' Look up namespaces in a C++
book, I can't be bothered to explain this for the umpteenth time.
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
cout<<"paratmerized constructor called";
}
Get rid of the above constructor, add default parameters to the next
constructor (see below). The above constructor makes no sense because it
doesn't initialise anything.
Matrix( int rows , int cols ) ;
Matrix(int rows = 0, int cols = 0);
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();
This is wrong, simple syntax error

Matrix operator+(Matrix add);
>
};
Matrix :: Matrix ( int rows = 0 , int cols = 0)
This is wrong

Matrix :: Matrix ( int rows , int cols)

If you want default parameters put them in the class, not in the definition.
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;
}
}
}
void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";
}

}
//cout<<"Enter the 2nd Matrix"<<endl;
}
void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;
}
cout << "|" << endl ;
}

cout<<'\n';
cout<<'\n';
}

Matrix :: Matrix operator + (Matrix add)
Matrix Matrix :: operator + (Matrix add)

Again, simple syntax mistake.
{
Matrix temp;
temp.numCols=numCols + add.numCols;
temp.numRows=numRows + add.numRows;
The above is not bad C++, but is makes no sense mathematically. Do you
know how to add two matrixes?
return temp;
}

void main ( )
int main()

Main always returns an int, that's a rule of C++. A stupid rules, but
still a rule.
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatrix ( ) ;
matrix2.getMatrix();
//Matrix = matrix1 + matrix2 ;
matrix1.displayMatrix ( ) ;
matrix2.displayMatrix ( ) ;
//
system ( "PAUSE" ) ;
getch();
}
Hope this helps.

john
Jun 23 '07 #6

P: n/a
On 23 Jun, 06:10, John Harrison <john_androni...@hotmail.comwrote:
You've made a few mistakes but they are just the usual beginner
mistakes, see below for some corrections.

You should try to write less code at once. When you write too much code
you just get errors on top of errors and it gets really confusing (you
know this). When you write code, just write a few more lines then try to
compile and run. Don't try to write thirty lines of code at once until
you get to be an expert. Don't try and write more code, when you don't
understand what's wrong with the code you've written. This is really
important advice, more important than anything else I tell you below.
To the OP: John is absolutely right here. More than anything, this is
your main problem.

<snip>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
cout<<"paratmerized constructor called";
}

Get rid of the above constructor, add default parameters to the next
constructor (see below). The above constructor makes no sense because it
doesn't initialise anything.
Matrix( int rows , int cols ) ;

Matrix(int rows = 0, int cols = 0);
That's not the right solution either. Originally the OP had
effectively this:

class Matrix
{
public:
Matrix() {}
Matrix(int rows = 0, int cols = 0) {}
};

int main()
{
Matrix m1(2, 2);
Matrix m2(2, 2);
}

I've left the constructor bodies empty becuase they aren't needed for
my example. That's definitely wrong because if you add

Matrix m3;

in main the code won't even compile because both the OP's constructors
can be called with zero arguments and so the compiler doesn't know
which one to call.

Your suggestion is to remove the one of the constructors:

class Matrix
{
public:
Matrix(int rows = 0, int cols = 0) {}
};

int main()
{
Matrix m1(2, 2);
Matrix m2(2, 2);
Matrix m3;
}

Now the above all compiles. No ambiguity. The statement Matrix m3;
calls the constructor with rows equal to 0 and cols equal to 0.
However, with default parameters, there are now *three* constructors
in class Matrix. The two argument constructor (used to construct m1
and m2 above), the zero argument constructor (used to construct m3
above) and a single argument constructor:

int main()
{
Matrix m4(2);
}

m4 is constructed with rows equal to 2 and cols equal to 0. I'd be
surprised if the OP needs this.

Worse, because the constructor is not explicit, the following compiles
as well:

int main()
{
Matrix m5(2, 2);
int foo = 42;

// ... lots of intervening code

m5 = foo; // Do you REALLY want to be able to do this?
}

After the statement m = foo; whatever was in m has gone and been
replaced with a brand new Matrix with 42 rows and no columns.

The OP needs to ditch the default parameters completely and go with
two constructors (I've only worried about the numRows and numCols
members for brevity):

class Matrix
{
public:
Matrix();
Matrix(int rows, int cols);
private:
int numRows;
int numCols;
};

Matrix::Matrix() : numRows(0), numCols() {}

Matrix::Matrix(int rows, int cols) : numRows(rows), numCols(cols) {}

int main()
{
Matrix m1(2, 2); // OK
Matrix m2(2, 2); // OK
Matrix m3; // OK - OP's code couldn't do this but it looked like
they wanted to be able to.
Matrix m4(2); // Won't compile - your code allowed this.

Matrix m5(2, 2);
int foo = 42;
m5 = foo; // Won't compile - your code allowed this.
}

Gavin Deane

Jun 23 '07 #7

P: n/a
John Harrison wrote:
You've made a few mistakes but they are just the usual beginner
mistakes, see below for some corrections.
mo**************@gmail.com wrote:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

Replace the above with

#include <iostream>
#include <conio.h>
using namespace std;

There is no header file called <iostream.hin C++.
But you think there is one called <conio.h>?


Brian
Jun 23 '07 #8

P: n/a
On 23 Jun, 09:40, Gavin Deane <deane_ga...@hotmail.comwrote:
Matrix::Matrix() : numRows(0), numCols() {}
Oops. Should be:

Matrix::Matrix() : numRows(0), numCols(0) {}
^

Gavin Deane

Jun 23 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.