![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Matrix Class ... Operator Problem
Hello, this is my code for matrix class, but its showing a runtime error when i use the overloaded operator() to assign values to a location.
[php] #include<iostream> #include<iomanip> #include<cassert> #define ROWS 7 #define COLS 7 namespace std { using namespace std; class Matrix { friend ostream& operator<< ( ostream& , const Matrix& ); friend istream& operator>> ( istream& , const Matrix& ); public: Matrix( unsigned int = 1, unsigned int = 1 ); ~Matrix(); void Clear(); // inline Matrix Transpose( ); private: unsigned int rows; unsigned int cols; double *container; public: Matrix operator+ ( const Matrix& ) const; Matrix operator- ( const Matrix& ) const; Matrix operator* ( const Matrix& ) const; Matrix operator/ ( const Matrix& ) const; void operator() ( unsigned int, unsigned int, double ); double operator() ( unsigned int , unsigned int ) const; }; Matrix::Matrix( unsigned int r, unsigned int c ):rows( r ),cols( c ) { assert( rows == 0 || cols == 0 || rows <= ROWS || cols <= COLS ); container = new double[rows*cols]; container = 0x00; } Matrix::~Matrix() { delete[] container; } Matrix Matrix::operator+ ( const Matrix& N ) const { Matrix Temp(rows*cols); if( rows == N.rows && cols == N.cols ) for( int i=0 ; i <= rows*cols ; i++ ) Temp.container[i] = container[i] + N.container[i]; return Temp; } double Matrix::operator() (unsigned int r, unsigned int c) const { if ( r > rows || c > cols ) cerr << "Error: Matrix subscript out of bounds"; return container[c*r]; } void Matrix::operator() ( unsigned int r, unsigned int c, double value ) { if ( r > rows || c > cols ) cerr << "Error: Matrix subscript out of bounds"; container[c*r] = value; } Matrix Matrix::Transpose( ) { Matrix result(rows*cols); for( int i = 0; i <= rows*cols ; i++ ) result.container[i] = container[i]; return result; } }; int main() { std::Matrix m(2,2); std::Matrix n; n(1,1,9); /*Showing error at this statement*/ system("PAUSE"); } [/php] |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Your actual problem is here;
Matrix::Matrix( unsigned int r, unsigned int c ):rows( r ),cols( c )
{
assert( rows == 0 || cols == 0 || rows <= ROWS || cols <= COLS );
container = new double[rows*cols];
container = 0x00;
}Incidentally, the C++ standard explicitly disallows placing user written code in namespace std. Last edited by grumpy; Dec 2nd, 2005 at 4:25 PM. Reason: Disable smilies |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Quote:
and no C++ doesnt disallows placing code in namespace std. It's working on my side. |
|
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
17.4.3.1 Reserved names 1 It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std. Such a specialization (complete or partial) of a standard library template results in undefined behavior unless the declaration depends on a userdefined name of external linkage and unless the specialization meets the standard library requirements for the original template. 2 The C++ Standard Library reserves the following kinds of names: — Macros — Global names — Names with external linkage 3 If the program declares or defines a name in a context where it is reserved, other than as explicitly allowed by this clause, the behavior is undefined. Your code is adding declarations and definitions to namespace std (creating a class named std::Matrix), but that is not one of the allowable circumstances for doing so. You are invoking undefined behaviour according to the standard. |
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Grumpy it seems to me like you know the C++ and C standards by heart...
.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
I've picked up a few bits and pieces in practice, and in discussion with some gurus. I don't have a memory like an encyclopedia, but I am a professional researcher so have some experience digging out details when I wish to. |
|
|
|
|
|
|
#7 | ||
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Quote:
I dont know that from where did you got that, but I am sorry to say that it isnt true. I have tested my file on Visual C++ 6 and Borland C++ Builder 5.5. Both of these compilers follow the standrad, so either the article you posted could be a misinformation, or you(or the writer of that article) might not be categorizing Visual C++ and Borland C++ as the Standard C++ Compilers. Here's something for you, from Bruce Eckel's Thinking in C++, Chapter 10, concerning namespaces: Quote:
|
||
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Mar 2005
Posts: 40
Rep Power: 0
![]() |
Quote:
pss ... its a namespace, not a class, you cannot disallow the extension in its definition. Last edited by Planet_EN; Dec 5th, 2005 at 2:49 PM. |
|
|
|
|
|
|
#9 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
|
|
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
Quote:
VC++ 6.0 was written before the full standard was in effect. It does the wrong thing in several places. E.g.: for (int i = 0; i < 10; i++) {
// Do something
}
for (int i = 0; i < 10; i++) {
// Do something else.
}As for Borland C++ Builder, here's a support thread on Borland's own website for you to read. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|