Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 2nd, 2005, 3:56 PM   #1
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
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]
Planet_EN is offline   Reply With Quote
Old Dec 2nd, 2005, 4:25 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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; 
}
which assigns container to be zero i.e. the NULL pointer. Any dereference of container (eg container[c*r] = something;) will therefore yield undefined behaviour.

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
grumpy is offline   Reply With Quote
Old Dec 3rd, 2005, 1:57 PM   #3
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
Quote:
Originally Posted by grumpy
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; 
}
which assigns container to be zero i.e. the NULL pointer. Any dereference of container (eg container[c*r] = something will therefore yield undefined behaviour.

Incidentally, the C++ standard explicitly disallows placing user written code in namespace std.
I fixed the bug last night...
and no C++ doesnt disallows placing code in namespace std. It's working on my side.
Planet_EN is offline   Reply With Quote
Old Dec 3rd, 2005, 4:31 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by Planet_EN
and no C++ doesnt disallows placing code in namespace std. It's working on my side.
Your compiler may allow it (some do) but that does not mean it is kosher. Rather than debate, I'll simply quote the source (some formatting lost due to cut and paste from the C++ standard).....


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.
grumpy is offline   Reply With Quote
Old Dec 3rd, 2005, 6:23 PM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Dec 3rd, 2005, 7:46 PM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by nnxion
Grumpy it seems to me like you know the C++ and C standards by heart....
Naah. Nobody could do that.

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.
grumpy is offline   Reply With Quote
Old Dec 5th, 2005, 2:22 PM   #7
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
Quote:
Originally Posted by grumpy
Your compiler may allow it (some do) but that does not mean it is kosher. Rather than debate, I'll simply quote the source (some formatting lost due to cut and paste from the C++ standard).....


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.

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:
* A namespace definition can appear only at global scope, or nested within another namespace.
* No terminating semicolon is necessary after the closing brace of a namespace definition.
* A namespace definition can be “continued” over multiple header files using a syntax that, for a class, would appear to be a redefinition:
[php]
//: C10:Header1.h
#ifndef HEADER1_H
#define HEADER1_H
namespace MyLib {
extern int x;
void f();
// ...
}
#endif // HEADER1_H ///:~


//: C10:Header2.h
#ifndef HEADER2_H
#define HEADER2_H
#include "Header1.h"
// Add more names to MyLib
namespace MyLib { // NOT a redefinition!
extern int y;
void g();
// ...
}
#endif // HEADER2_H ///:~


//: C10:Continuation.cpp
#include "Header2.h"
int main() {} ///:~
[/php]
Planet_EN is offline   Reply With Quote
Old Dec 5th, 2005, 2:34 PM   #8
Planet_EN
Programmer
 
Join Date: Mar 2005
Posts: 40
Rep Power: 0 Planet_EN is an unknown quantity at this point
Quote:
Originally Posted by grumpy
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.
No, not the undefined behaviour, the undefined behaviour will not be invoked by it. Just like all other namespaces, namespace std is just another namespace, nothing else ... it doesnt disallows explicit definition, infact I quoted the text from Eckel's book above that says that namespace definition could be extended upto many header files.

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.
Planet_EN is offline   Reply With Quote
Old Dec 5th, 2005, 4:31 PM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by Planet_EN
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.
Visual C++ 6 was written before the standard was released in 1998, so I doubt it follows it. I don't know about Borland.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 5th, 2005, 7:00 PM   #10
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
Quote:
Originally Posted by Planet_EN
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.
It's in his post. It's from the C++ standard, specifically section 17.4.3.1 Reserved names of the C++ Standard.

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.
}
VC++ 6.0 will refuse to compile the above code claiming that i is already declared, whereas the C++ standard says it should lose scope outside the loop and hence should be declared again. There are many such places where VC++ 6.0 doesn't conform. Not Microsoft's fault though... they released that compiler before the full standard was in place. By the way, VC++ 7.xx is much more (but not fully) standards compliant.

As for Borland C++ Builder, here's a support thread on Borland's own website for you to read.
Scorpions4ever is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC