Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ template operator+ how to make diffrent instances work with it
    primarykey
    data
    text
    <p>First I have Matrix template Matrix&lt; 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? </p> <p>Code of Matrix templates below:</p> <pre><code>template&lt;typename T,int Roz&gt; class Matrix { public: T tab[Roz][Roz]; int z=Roz; Matrix() { for(int i=0;i&lt;Roz;++i) for(int j=0;j&lt;Roz;++j) tab[i][j]=0; } T&amp; operator()(int x,int y){ return tab[x-1][y-1]; } //Problematic operator Matrix&amp; operator+(Matrix&lt;T,Roz&gt; b) { Matrix&lt;T,Roz&gt; tmp; for(int i=0;i&lt;Roz;++i) for(int j=0;j&lt;Roz;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } friend ostream&amp; operator&lt;&lt;(ostream&amp; out, Matrix&lt;T,Roz&gt; &amp;v) { for(int i=0;i&lt;v.z;++i) { for(int j=0;j&lt;v.z;++j) out&lt;&lt;v.tab[i][j]&lt;&lt;" "; out&lt;&lt;endl; } return out; } }; //Specialization template template&lt;class T&gt; class Matrix&lt;T,0&gt; { private: Matrix() { } public: vector&lt;vector&lt;T&gt; &gt; tab; int z=0; Matrix(int z) { for(int i=0;i&lt;z;++i) tab.push_back(vector&lt;T&gt;(z)); this-&gt;z = z; for(int i=0;i&lt;z;++i) for(int j=0;j&lt;z;++j) tab[i][j]=0; } T&amp; operator()(int x,int y){ return tab[x-1][y-1]; } //Problematic operator Matrix&amp; operator+(Matrix&lt;T,0&gt; b) { Matrix&lt;T,0&gt; tmp(b.z); for(int i=0;i&lt;z;++i) for(int j=0;j&lt;z;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } //Problematic operator Matrix&amp; operator+(Matrix&lt;T,0&gt; &amp;b) { Matrix&lt;T,0&gt; tmp(b.z); for(int i=0;i&lt;z;++i) for(int j=0;j&lt;z;++j) tmp.tab[i][j]=this-&gt;tab[i][j]+b.tab[i][j]; } friend ostream&amp; operator&lt;&lt;(ostream&amp; out, Matrix&lt;T,0&gt; &amp;v) { for(int i=0;i&lt;v.z;++i) { for(int j=0;j&lt;v.z;++j) out&lt;&lt;v.tab[i][j]&lt;&lt;" "; out&lt;&lt;endl; } return out; } }; </code></pre> <p>When I try add for example Matrix &lt; int,3 > and Matrix &lt; 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? </p> <p>The code below makes error when I try add A + Z.</p> <pre><code>int main() { Matrix&lt;int,3&gt; A, B; Matrix&lt;int, 4&gt; C; Matrix&lt;int, 0&gt; Z(3); A(1,1)=1; B(1,1)=2; Z(1,1)=1; Z + A;//&lt;------- here error between diffrent instance of Matrix template } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload