Thread: *operator overloading: scalar matrix multiplication

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    *operator overloading: scalar matrix multiplication

    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;
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1) A constructor initializes the member variables. Where do your constructors initialize 'A'?

    2) What does this do:
    Code:
    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];
    
    	}
    Eliminating those lines, produces this:
    Code:
    matrix  matrix :: operator* (int a)
    
    {
    
    	matrix temp(x, y);
    
    	int num,num1, num2;
    
            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);
    
    }
    Do you see any problems? Try this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    	int** A;
    	A[3][3] = 0;
    
    	return 0;
    }
    Last edited by 7stud; 06-27-2005 at 12:02 PM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Make sure in your operator* implementation that you don't allocate over the this pointer like you do. You allocate new memory for the member variable A instead of temp.A

    Also, in at least one of your for loops you loop one too many:

    Code:
    for (num=0; num<=temp.x; num++)
    {
    For a 2x2 matrix, that loop will execute 3 times, for 0 , 1 and then 2. You probably just want < there I would imagine.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    thanks guys, I guess i was just too dumb and sleepy to notice these , it works fine now, I am gonna try matrix to matrix mul next with "*" overloading

    thanks a lot !!!!!

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    1
    hi.

    Can you post your complete working code, please.

    It could be of use to many people.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  2. Simple operator overloading issue
    By Desolation in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2007, 08:56 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  4. Need a quick example! Operator overloading and matrix
    By Great Satchmo in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 10:08 PM