Hi!

This is a small program i wrote where I am trying to overload the '*' operator to multiply a scalar to a matix and give a new matix.

The program compiles fine , reads and displays the matrix fine but when i multipy it with a scalar it gives me a segmentation fault.

I have tried functions like this b4 which return objects created locally inside the function to another object created in the main function, why is this not working ???

Here is the code:

Code:#include<iostream> class matrix { int **A; // pointer to store 2 dimensional array int x, y; public: matrix(); matrix(int, int); void read(); void input(); void display(); matrix operator* (int); }; matrix :: matrix() { x = 0; y = 0; } matrix :: matrix(int a, int b) { x = a; y = b; } void matrix :: read() { std::cout << " enter number of rows " << std::endl; std::cin >> x; std::cout << " enter number of columns " << std::endl; std::cin >> y; } void matrix :: input() { int num1, num2, k; int i, j; A = new int *[x]; for (k = 0; k < x; k++) A[k] = new int [y]; for (num1=0; num1<x; num1++) { for (num2=0; num2<y; num2++) { A[num1][num2] = 0; } } for ( i = 0; i < x; i++) { for ( j = 0; j < y; j++) { std::cin >> A[i][j]; } } } void matrix :: display() { int i, j; for (i=0; i<x; i++) { for (j=0; j<y; j++) { std::cout << " " << A[i][j]; } std::cout << "\n"; } } matrix matrix :: operator* (int a) { matrix temp(x, y); int num,num1, num2; A = new int *[temp.x]; for (num=0; num<=temp.x; num++) { A[num] = new int [temp.y]; } for (num1=0; num1<temp.x; num1++) { for (num2=0; num2<temp.y; num2++) { temp.A[num1][num2] = 0; } } int i, j; for ( i = 0; i < x; i++) { for ( j = 0; j < y; j++) { temp.A[i][j] = a * A[i][j]; } } return (temp); } int main () { matrix M1, M2; M1.read (); M1.input (); M1.display (); M2 = M1*2; M2.display(); return 0; }