question about template class
Jun 14, 2012 at 9:24am UTC
Why the following code can't be compiled? thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <cassert>
#include <iostream>
using namespace std;
template <typename T>
class Vector {
T * data;
int sz;
public :
Vector(int s) {
data = new T[s]();
sz = s;
}
template <typename N> Vector(const Vector<N> & 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 <typename N> Vector<T> & operator =(const Vector<N> & 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 <typename T1,typename T2,typename T3> friend Vector<T3> f(const Vector<T1> & a,const Vector<T2> & b);
};
template <typename T1,typename T2,typename T3>
Vector<T3> f(const Vector<T1> & a,const Vector<T2> & b) {
assert(a.sz == b.sz);
Vector<T3> 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<int > 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;
}
Jun 14, 2012 at 9:44am UTC
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:
1 2
template <typename T1,typename T2,typename T3>
Vector<T1 > f(const Vector<T2 > & a,const Vector<T3 > & 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);
Jun 14, 2012 at 10:22am UTC
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <cassert>
#include <iostream>
using namespace std;
template <typename T>
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 <typename N> Vector(const Vector<N> & 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 <typename N> Vector<T> & operator =(const Vector<N> & 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 <typename T1,typename T2> friend Vector<T1> f(const Vector<T1> & a,const Vector<T2> & b);
};
template <typename T1,typename T2>
Vector<T1> f(const Vector<T1> & a,const Vector<T2> & b) {
assert(a.sz == b.sz);
Vector<T1> 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<int > 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
Jun 14, 2012 at 12:00pm UTC
It works fine for me
Jun 14, 2012 at 4:04pm UTC
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.
Jun 14, 2012 at 5:05pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
template <typename N>
Vector(const Vector<N> & 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 <typename N>
Vector<T> & operator =(const Vector<N> & 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.