i write code but dont understand the error please help me

M

mohammaditraders

#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();
}
 
Z

Zachary Turner

#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?
 
I

Ian Collins

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!
 
B

BobR

My suggestions inline below:

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

#include <iostream>
#include <cstdlib>
#include said:
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 said:
}

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.
 
J

John Harrison

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.

#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.h> in 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.h> and <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
 
G

Gavin Deane

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.

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
 
D

Default User

John said:
You've made a few mistakes but they are just the usual beginner
mistakes, see below for some corrections.
Replace the above with

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

There is no header file called <iostream.h> in C++.

But you think there is one called <conio.h>?




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top