C++ How can I call a template function with a template class? -


i have quick question program: how can call template function set, rather int? have class here called set

#include <iostream> #include <vector>  using namespace std;  template<typename t>  class set { public:     class iterator;     void add(t v);     void remove(t v);     iterator begin();     iterator end();  private:     vector<t> data; };  

here's cpp:
unfortunately, main cannot template function had make function addstuff, main calls

template <class t> set<t> addstuff() {     set<t> a;     a.add(1);     a.add(2);     a.add(3);     a.add("a string");      return a; }  void main() {     addstuff<set>(); //<< error here. if use addstuff<int>(), run                         //i can't add string it. required able add                       //different data types vector } 

your writing addstuff<set>() attempt resolve set<set> addstuff() meaningless.

addstuff<std::string>() would allow add std::strings set, a.add(1) fail since literal cannot implicitly converted string type.

addstuff<int>() does work that's merry coincidence. add(1) has correct type in instance added set<int>.

you could build class foo has non-explicit constructors string , integer , make template type: addstuff<foo>(). i'm not convinced that's professor wants , there better ways of solving (type erasure one, getting quite involved).


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -