* operator overloading with template class
<p>Trying to achieve Matrix multiplication through overloaded * operator with class templates.The code works on CodeBlocks (with some warnings) in Windows ,but while compiling through g++ in Ubuntu it shows some issues.Furthurmore running the './a.out' yields segmentation fault.</p>

<pre><code>#include &lt;iostream&gt;
#define TOLERANCE 0.0001

using namespace std;

template &lt;class T&gt;class Matrix;
template &lt;class T&gt;ostream &amp;operator&lt;&lt;(ostream &amp;out,const Matrix&lt;T&gt;&amp;);
//Matrix can be of any data type
template &lt;class T&gt; Matrix&lt;T&gt; &amp;operator*( Matrix&lt;T&gt;&amp;);
template &lt;class T&gt;
class Matrix{
    private:
        int rows,columns;
        T **A;
    public:
        //Default Constructor
        Matrix(){rows=0;columns=0;}
        //Parameterised Constructor
        Matrix(int Rows,int Cols){
        rows=Rows;
        columns=Cols;
        A=new T*[rows];
        for(int i=0; i&lt;rows; i++)
            A[i]=new T[columns];
        }
        void displayMatrix();
        void fillMatrix(T x);
        Matrix&lt;T&gt;&amp; operator* ( Matrix&lt;T&gt; &amp;X);//Matrix Multiply
        friend ostream &amp;operator&lt;&lt; &lt;T&gt;(ostream &amp;out ,const Matrix&lt;T&gt; &amp;X);

};
template &lt;class T&gt;
void Matrix&lt;T&gt;::displayMatrix(){
    for(int i=0;i&lt;rows ;i++ ){
        for(int j=0 ;j&lt;columns ;j++ )
            cout&lt;&lt;A[i][j]&lt;&lt;" ";
        cout&lt;&lt;endl;
        }
    }

template &lt;class T&gt;
void Matrix&lt;T&gt;::fillMatrix(T x){
    for(int i=0; i&lt;rows; i++)
        for(int j=0; j&lt;columns; j++)
            A[i][j]=x;
}

template &lt;class T&gt;
Matrix&lt;T&gt; &amp;Matrix&lt;T&gt;::operator*(  Matrix&lt;T&gt; &amp;X){
    T sum=0;
    if(columns=X.rows){
        Matrix&lt;T&gt; Q(rows,X.columns);
        for(int i=0; i&lt;rows ;i++){
            for(int j=0; j&lt;X.columns; j++){
                for(int k=0 ;k&lt;columns ;k++ )
                    sum+=A[i][k]*(X.A[k][j]);
                Q.A[i][j]=sum;
                sum=0;
                }
            }
            return Q;
        }else{
        cout&lt;&lt;"Something's wrong!";
        return *this;
        }
    }

int main(){
Matrix&lt;double&gt; x,y,z;
x.fillMatrix(1.2);
y.fillMatrix(2.3);
z=x*y;
z.displayMatrix();
return 0;
}
</code></pre>

<p>I want to achieve Matrix Multiplication for generalized template in cpp.
This yields the following  :</p>

<blockquote>
  <p>myProj.cpp: In instantiation of ‘Matrix&amp; Matrix::operator*(Matrix&amp;) [with T = int]’:
  myProj.cpp:75:5:   required from here
  myProj.cpp:52:13: warning: reference to local variable ‘Q’ returned [-Wreturn-local-addr]
     Matrix Q(rows,X.columns);</p>
</blockquote>