i have a class which has a template by other purposes:
template<class t>
class MyClass {
public: //of course public...
t foo;
std::string text;
}
and i have another class which method get all kind of these class through the arguments, and want to store the pointer in an array. The class dont want to access the specific (tempalted) parts of the classes only the common attributes/methods.
class Container {
public: //of course public...
MyClass* array; //this is allocated with some magic.
void bar(MyClass& m) {
and want to store the class in a MyClass* array.
}
}
here is the error that argument list for template missing
how can i solve this?
the text variable in your class is private so unless you bar function is a method of the class you can't legally use it like that
If you want to create a "multi-template" array, you'd better use a non-template class as a base class of a template class. Or you can make a template array and store any objects in it.
The simplest method would be to make that function a template as well:
Note that that should probably be
const MyClass<t>&
, because you don't need to modify it.Your new code is meaningless. There is no such that as an object of type
MyClass
, becauseMyClass
is a template. If you want to operate on these classes irrespective of their template argument, then you need to factor out the non-template portions as a base class:Then you can refer to that base, and when you call a virtual function it will dynamically dispatch:
How about putting a common base class.