445,743 Members | 1,072 Online
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

 P: n/a #include #include #include #include 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"<
8 Replies

 P: n/a On Jun 22, 7:59 pm, mohammaditrad...@gmail.com wrote: #include #include #include #include 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"<

 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 // #include // #include // #include #include #include #include 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"<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 #include #include #include Replace the above with #include #include using namespace std; There is no header file called without the '.h'. There are header file called 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"<

 P: n/a On 23 Jun, 06:10, John Harrison 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 #include #include #include Replace the above with #include #include using namespace std; There is no header file called ? Brian Jun 23 '07 #8

 P: n/a On 23 Jun, 09:40, Gavin Deane

### This discussion thread is closed

Replies have been disabled for this discussion.