Overload Operator Recieving Segment Fault
Feb 16, 2012 at 1:29am UTC
I have a function that checks for a specific parameter, if that parameter is incorrect i need it to send a null pointer or something to an overloaded ostream operator.
within the ostream operator it will check for this null pointer and print an error message otherwise it will print the normal desired results..
my problem lies when I try to run this on a unix based system and i get a "Segmentation Fault" error message and it aborts the program.
What way would you go around this?
My function to check for error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Matrix Matrix::operator *(const Matrix& test)
{
if ( test.num_cols() == this ->num_rows()) {
Matrix tmp(num_rows(), test.num_cols());
for (int i = 0; i < num_rows(); i++){
for (int j = 0; j < test.num_cols(); j++){
for (int l = 0; l < num_cols(); l++){
tmp.mydata[i][j] += this ->mydata[i][l] * test.mydata[l][j];
}
}
}
return tmp;
}
else {
return *this ; //need this to return a null pointer.. its *this to avoid the seg error
//Matrix *ptr = NULL;
//return *ptr;
}
}
ostream overloaded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
ostream& operator << (ostream& os, const Matrix& test) {
if (test == NULL){
os << "Error" ;
}
else
{
for (int i = 0; i < test.num_rows(); i++){
for (int j = 0; j < test.num_cols(); j++){
os << setw(6) << test.get_element(i, j) << " " ;
}
os << "\n" ;
}
os << endl;
}
return os;
}
Feb 16, 2012 at 2:52am UTC
You were definitely playing with fire there.
I would suggest throwing an exception.
Feb 16, 2012 at 4:44am UTC
how do i throw an exception to the ostream? there is pretty poor documentation, at least for my understanding on try/catch/throw methodology
Feb 16, 2012 at 9:35am UTC
Hi
Either do it by using
assert( test.num_cols() == this ->num_rows() );
, or excetiopn
If you still dont want to use niether of them, than
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Matrix Matrix::operator *(const Matrix& test)
{
Matrix tmp;// default constructor must set col and row-sizes = 0
// and allocate no memory
if ( test.num_cols() == this ->num_rows()) {
tmp.resize(num_rows(), test.num_cols()); // resize must be availble
for (int i = 0; i < num_rows(); i++){
for (int j = 0; j < test.num_cols(); j++){
for (int l = 0; l < num_cols(); l++){
tmp.mydata[i][j] += this ->mydata[i][l] * test.mydata[l][j];
}
}
}
}
return tmp;
}
and
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
ostream& operator << (ostream& os, const Matrix& test) {
if (test ==0){ // you must overload operator==(int ), or do the folllowing check
//if( test.num_rows() == 0 || test.num_cols() == 0 ){
os << "Error" ;
}
else
{
for (int i = 0; i < test.num_rows(); i++){
for (int j = 0; j < test.num_cols(); j++){
os << setw(6) << test.get_element(i, j) << " " ;
}
os << "\n" ;
}
os << endl;
}
return os;
}
Last edited on Feb 16, 2012 at 9:37am UTC
Feb 16, 2012 at 1:05pm UTC
Thanks, didn't even think of doing it that way :)
good call
Topic archived. No new replies allowed.