I am trying to create a template that will allow me to use the resizable array. Is there any way to find sizeof (T)? I use malloc, not the new one, because I want to use realloc in a function that resizes the array. This is the constructor for my class that gets errors:
template <class T> set<T>::set(void) { arr = malloc(10 * sizeof(T)); numElts = 0; size = 10; };
When you try to build, the following error message appears:
error C2440: '=' : cannot convert from 'void *' to 'int *' 1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast 1> c:\set.cpp(42) : while compiling class template member function 'set<T>::set(void)' 1> with 1> [ 1> T=int 1> ]
In the main function, I call it with:
set<int> *set1 = new set<int>();
From the research I did, it looks like the compiler does not know what to use for sizeof (T), so it cannot compile. How else will I do this?
c ++ templates
Dom12 Nov 03 '11 at 10:18 2011-11-03 22:18
source share