Temple of The Roguelike Forums
Development => Programming => Topic started by: Krice on August 21, 2010, 07:36:11 PM
-
Feeling stupid again. What's wrong with this picture:
template<typename T>
void load_object_list(list<T> &v, K_File *sf)
{
Tar_Ball tb;
tb.Load(sf);
int s=tb.Get_Next_Value();
for (int t=0; t<s; t++)
{
T *o=new T;
o->Load(sf);
v.push_back(o);
}
}
I'm passing T as:
std::list<U_Door*> v_doors;
load_object_list(v_doors, sf);
That T *o=new T is failing. Damn templates! I hate 'em.
-
Dont even know what language this is but Have you tested to make sure its entering the for loop at all? also if list<U_door*> is a Cast and list<T> is a cast you have made me verry confused. as why your passing load_object_list() a U_door* List when it needs a T list...
Otherwise I cant help since Im guessing how any of this works haha!
-
You pass list<U_Door*> as argument.
So your T is 'U_Door*'.
T* is U_Door**.
U_Door** o=new (U_Door*);
is syntactically wrong.
Look at this:
template <class T>
struct GetType{
typedef T Type;
};
template <class T>
struct GetType<T*>{
typedef T Type;
};
template <class T>
class A{
};
template <class T>
void test(A<T>& a)
{
typename GetType<T>::Type* t=new typename GetType<T>::Type;
}
int main()
{
A<int*> a;
A<int> b;
test(a);
test(b);
}
-
That example confused me more. I just want to create a new object type T. How to do that in this case?
-
In example there are template structure and specialization of the same template structure for pointers.
It's the way to convert T* to T.
Basically GetType<int*>::Type is int.
And GetType<int>::Type is int too.
But you need to add keyword typename before GetType<int>::Type.
Add GetType structures to your code, and change this line:
T *o=new T;
to this:
typename GetType<T>::Type* o=new typename GetType<T>::Type;
-
That was it. I have no idea why such complicated stuff is needed, but if it actually works then I care not.
-
It was a lil too late. So I solved different problem :)
Try it this way:
template<typename T>
void load_object_list(list<T*> &v, K_File *sf)
{
Tar_Ball tb;
tb.Load(sf);
int s=tb.Get_Next_Value();
for (int t=0; t<s; t++)
{
T *o=new T;
o->Load(sf);
v.push_back(o);
}
}
It's ALWAYS list of pointers. You can't pass list of values anyway.
-
I was about to ask what is the difference with T and T* because I had a routine where I used T* and then changed it to T. Both compile fine, but which one is "correct"?
template<typename T>
void Refresh_Object_Map(list<T> &v)
{
for (typename list<T>::iterator pos=v.begin(); pos!=v.end(); pos++) (*pos)->Put_To_Map();
}
-
In this case it doesn't really matter.
If you make list<T*> and try to pass list<SomeClass>, compilation
error will be on call site, and in case of list<T> compilation error
will be during template instantiation, and as a result a little harder to track.
Btw this way:
template<typename T>
void Refresh_Object_Map(list<T> &v)
{
for (typename list<T>::iterator pos=v.begin(),end=v.end(); pos!=end; ++pos) (*pos)->Put_To_Map();
}
is a little bit more effective.
If you do not modify list in a cycle (which is bad idea anyway).