C++ Template Class definition getting error -
i learning c++ templates.i created template class addition of 2 strings i'm getting folloing error: please me understand error.
main.cc:65:52: error: no matching function call thenameholder<std::basic_string<char> >::thenameholder(const char [8], const char [7])
using namespace std; template <class t> class thenameholder{ t *a, *b; public: thenameholder(t *first, t *last) { a= first; b= last; } t getname(); }; template <class t> t thenameholder<t> :: getname() { t returnval; returnval = strcat (returnval,a); returnval = strcat (returnval, " "); returnval = strcat (returnval, b); return returnval; } int main() { thenameholder <string> obj ("hi", ""); cout << obj.getname (); return 0; }
what? no. isn't templates used for
you use strcat
on templated objects (actually, on t*
, on pointers object)
strcat
accepts char *
. t
has char work. if know t
char
, isn't template know is. (btw - have bug returnval
should t*
, , using uninitialized)
you seem miss whole concept of templates - ok since learning.
see - sentence "i want use templates add 2 strings" wrong - don't have unknowns! want add 2 strings, know type is. isn't template.
template "i want add 2 lists of unknown type" (then can't use strcat
obviously, nor can assume lists "zero delimited" true c style strings).
Comments
Post a Comment