Why the following code can't be compiled? thanks!
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include using namespace std; template class Vector { T * data; int sz; public: Vector(int s) { data = new T[s](); sz = s; } template Vector(const Vector & t) { int * tmp = new T[sz](); for(int i = 0 ; i < sz ; i++) tmp[sz] = t.data[i]; delete[] data; data = tmp; } T & operator()(int i) { assert(0 <= i && i < sz); return data[i]; } template Vector & operator=(const Vector & t) { int * tmp = new T[sz](); for(int i = 0 ; i < sz ; i++) tmp[sz] = t.data[i]; delete[] data; data = tmp; return *this; } template friend Vector f(const Vector & a,const Vector & b); }; template Vector f(const Vector & a,const Vector & b) { assert(a.sz == b.sz); Vector retVal(a.sz); for(int i = 0 ; i < a.sz ; i++) retVal.data[i] = a.data[i] + b.data[i]; return retVal; } int main() { Vector t1(3),t2(3),t3(3); for(int i = 0 ; i < 3 ; i++) { t1(i) = i; t2(i) = i; } t3 = f(t1,t2); return 0; }``````
Try this:

Line 32:
`template<typename T1,typename T2,typename T3> friend Vector<T1> f(const Vector<T2> & a,const Vector<T3> & b);`

Line 35:
 ``12`` ``````template Vector f(const Vector & a,const Vector & b) {``````

Line 49:
`t3 = f<int>(t1,t2);`

The problem in your code is that the compiler can not resove T3 hence you'd provide all template parameter until T3: `t3 = f<int, int, int>(t1,t2);`
Thanks for your reply. I restricted the type of the return value. And again met a error when running the program. The error is
 ``` *** glibc detected *** ./a.out: double free or corruption (fasttop): 0x0000000000c19050 *** ```

the code is
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````#include #include using namespace std; template class Vector { T * data; int sz; public: Vector() : data(NULL),sz(0) { } Vector(int s) { data = new T[s](); sz = s; } ~Vector() { if(data) delete[] data; } template Vector(const Vector & t) { int * tmp = new T[sz](); for(int i = 0 ; i < sz ; i++) tmp[sz] = t.data[i]; if(data) delete[] data; data = tmp; } T & operator()(int i) { assert(0 <= i && i < sz); return data[i]; } template Vector & operator=(const Vector & t) { int * tmp = new T[sz](); for(int i = 0 ; i < sz ; i++) tmp[sz] = t.data[i]; delete[] data; data = tmp; return *this; } template friend Vector f(const Vector & a,const Vector & b); }; template Vector f(const Vector & a,const Vector & b) { assert(a.sz == b.sz); Vector retVal(a.sz); for(int i = 0 ; i < a.sz ; i++) retVal.data[i] = a.data[i] + b.data[i]; return retVal; } int main() { Vector t1(3),t2(3),t3; for(int i = 0 ; i < 3 ; i++) { t1(i) = i; t2(i) = i; } t3 = f(t1,t2); return 0; }``````

How does the double free happen? Thanks
It works fine for me
In the copy assignment operator you never update sz so the old value is still used. tmp[sz] is out of bounds.

In the copy constructor you have similar problems. sz and data is never initialized.
 ``12345678910111213141516171819`` `````` template Vector(const Vector & t) { int * tmp = new T[sz](); for(int i = 0 ; i < sz ; i++) tmp[sz] = t.data[i]; if(data) delete[] data; data = tmp; } template Vector & operator=(const Vector & t) { int * tmp = new T[sz](); for(int i = 0 ; i < sz ; i++) tmp[sz] = t.data[i]; delete[] data; data = tmp; return *this; }``````

They bug me because the template parameters T and N are not necessarily the same - therefore you could end up could be trying to construct
a Vector<std::string> from a Vector<int> which makes no sense.
Also
` int * tmp = new T[sz](); //really??? `
Topic archived. No new replies allowed.