0

First I have Matrix template Matrix< typename T, int Roz> and specialization of this template Matrix. But the specialization is create with constructor where argument is a size of matrix. So in example code A and Z have the same dimension. So I want to make add operator work with it. But complier say: error: no match for 'operator+' in 'Z + A'. So how I need to write operator+ for Matrix wich will be work with Matrix?

Code of Matrix templates below:

template<typename T,int Roz>
    class Matrix
    {
    public:
    T tab[Roz][Roz];
    int z=Roz;
    Matrix()
    {
        for(int i=0;i<Roz;++i)
                for(int j=0;j<Roz;++j)
                    tab[i][j]=0;
    }
    T& operator()(int x,int y){
        return tab[x-1][y-1];
    }
            //Problematic operator
    Matrix& operator+(Matrix<T,Roz> b)
    {
            Matrix<T,Roz> tmp;
            for(int i=0;i<Roz;++i)
                for(int j=0;j<Roz;++j)
                    tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];

    }


    friend ostream& operator<<(ostream& out, Matrix<T,Roz> &v)
    {

        for(int i=0;i<v.z;++i)
                {
                    for(int j=0;j<v.z;++j)
                        out<<v.tab[i][j]<<" ";

                    out<<endl;
                }
        return out;
    }
    };

    //Specialization template
template<class T>
class Matrix<T,0> 
{
private:
     Matrix()
    {

    }
public:
    vector<vector<T> > tab;
    int z=0;

    Matrix(int z)
    {
        for(int i=0;i<z;++i)
            tab.push_back(vector<T>(z));
        this->z = z;
        for(int i=0;i<z;++i)
                for(int j=0;j<z;++j)
                    tab[i][j]=0;
    }
    T& operator()(int x,int y){
        return tab[x-1][y-1];
    }
            //Problematic operator
    Matrix& operator+(Matrix<T,0> b)
    {
            Matrix<T,0> tmp(b.z);
            for(int i=0;i<z;++i)
                for(int j=0;j<z;++j)
                    tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];

    }
            //Problematic operator  
     Matrix& operator+(Matrix<T,0> &b)
    {
            Matrix<T,0> tmp(b.z);
            for(int i=0;i<z;++i)
                for(int j=0;j<z;++j)
                    tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];




    }


    friend ostream& operator<<(ostream& out, Matrix<T,0> &v)
    {

        for(int i=0;i<v.z;++i)
                {
                    for(int j=0;j<v.z;++j)
                        out<<v.tab[i][j]<<" ";

                    out<<endl;
                }
        return out;
    }
};

When I try add for example Matrix < int,3 > and Matrix < int, 0 > this make error. How I must definie operator+ in both templates they will work together?May I have to overload operator+ in specialization template?

The code below makes error when I try add A + Z.

int main()
{
Matrix<int,3> A, B;
Matrix<int, 4> C;
Matrix<int, 0> Z(3);
A(1,1)=1;
B(1,1)=2;
Z(1,1)=1;


Z + A;//<------- here error between diffrent instance of Matrix template 
}
Aku
  • 200
  • 3
  • 9
  • 17

1 Answers1

0

I don't really know why you wish to add up matrices of different dimensions, but in order to do it, you must turn the method into a method template on the dimension of the matrix argument:

#include <type_traits>

template <int Roz2>
Matrix& operator+(Matrix<T,Roz2> b)
{
        // check matrix argument size compatibility
        std::static_assert(Roz2 >= Roz, "incompatible matrices for operator+()"); 
        Matrix<T,Roz> tmp;
        for(int i=0;i<Roz;++i)
            for(int j=0;j<Roz;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}

Since Matrix<T,0> is a specialization to handle dynamic dimension, you must specialize the operator for it:

// in the generic template
Matrix& operator+(Matrix<T,0> b)
{
        assert (b.z < Roz, "incompatible dynamic matrix for operator+");
        Matrix<T,0> tmp(b.Roz);;
        for(int i=0;i<Roz;++i)
            for(int j=0;j<Roz;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}

and in Matrix<T,0>:

template <int Roz>
Matrix& operator+(Matrix<T,Roz> b)
{
        assert(Roz >= z,"....");
        Matrix<T,0> tmp(b.z);
        for(int i=0;i<z;++i)
            for(int j=0;j<z;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}

In the above solution, however, the operator is not commutative. In order to make it so, we need to do some change:

 // define a min operator as constexpr in case the existing one is not 
template<typename T> constexpr
T const& rmin(T const& a, T const& b) {
    return a > b ? b : a;
}

// generic operator
template <int Roz2>
Matrix<T,rmin(Roz,Roz2)>& operator+(Matrix<T,Roz2> b)
{
        constexpr int R = min(Roz,Roz2);
        Matrix<T,R> tmp;
        for(int i=0;i<R;;++i)
            for(int j=0;j<R;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}

// specialized operator on Roz=0 argument
Matrix<T,0>& operator+(Matrix<T,0> b)
{
        const int r = min(z,b.z);
        Matrix<T,0> tmp(r);
        for(int i=0;i<r;;++i)
            for(int j=0;j<r;++j)
                tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
}

// specialized operator in Roz=0 template
   Matrix& operator+(Matrix<T,Roz> b)
{
        return b + *this;
}

You will have to duplicate the code for const & parameters taking operators (or perhaps simply get rid of the non const parameter versions which don't seem very useful in that context).

Note the use of a static assert to limit the use of the operator with matrices of equal or larger dimension (this is a C++11 feature) in the non commutative version of the operators.

See this question on the topic of min not being a constexpr function template for further details.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52
  • They have the same dimensions. The base tamplate have size in tamplate but the spacialization have zero in template but constructor have argument wich we set the size of matrix. So they have the same dimensions but make in different ways. – Aku Apr 08 '13 at 07:10
  • just cosmetics: I think you have spurious comma in line 12 of the last snippet – quetzalcoatl Apr 08 '13 at 07:53
  • Thanks thats works smoothly. I know my first post is not clear and problem not well explain. Sorry for that mistake. – Aku Apr 08 '13 at 09:26