c++ - Only inline template works, how to do it right -


i wrote myself stringhelper in cpp convert. won't compile if put sourceode in external cpp-file (included in codeblocks-projectfile) or don't understand errors:

hpp:

#ifndef _input_stringhelper_hpp     #define _input_stringhelper_hpp      #include <string>     #include <sstream>     #include <deque>      namespace fivedimension     {         void splitstream(std::stringstream& s, char c, std::deque<std::string>& ret);         void splitstring(std::string s, char c, std::deque<std::string>& ret);         template<typename t> t stringtoall(std::string val);         template<typename t> bool trystringtoall(std::string val, t &ret);         template<typename t> std::string alltostring(t val);     }  #endif 

cpp:

#include "stringhelper.hpp"  void fivedimension::splitstream(std::stringstream& s, char c, std::deque<std::string>& ret) {     std::string line;      while(std::getline(s, line, c))         ret.push_back(line); } void fivedimension::splitstring(std::string s, char c, std::deque<std::string>& ret) {     std::string line;     std::stringstream ss(s);      while(std::getline(ss, line, c))         ret.push_back(line); } template<typename t> t fivedimension::stringtoall(std::string val) {     std::stringstream s(val);     t ret;     s >> ret;     return ret; } template<typename t> bool fivedimension::trystringtoall(std::string val, t &ret) {     std::stringstream s(val);     return (s >> ret); } template<typename t> std::string fivedimension::alltostring(t val) {     std::stringstream s;     s << val;     return s.str(); } 

i tried example:

template<typename t> std::string fivedimension::alltostring<t>(t val) {     std::stringstream s;     s << val;     return s.str(); } 

but doesn't compile file , make me feel don't know templates came here. read answer aaron on topic: "undefined reference to" template class constructor . after understood lot more. how can predefine function ?

i'll brief of answer, in topic, you've mentioned: there 2 ways out of problem:

  1. use inlines (i know, don't want to, it's more general , correct way).
  2. use definitions of types you'll using in .cpp file.

for template classes should use following code:

template class your_class_name < typename_to_use >; 

where typename_to_use int, std::string or else. template functions should use following code:

template return_type your_function_name<typename_to_use>(...parameters); 

for template member functions should use following code:

template return_type your_class_name::your_function_name<typename_to_use>(...parameters); 

or

template return_type your_class_name<class_typename_to_use>::your_function_name<function_typename_to_use>(...parameters); 

the last case describing situation when template class uses template function.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -