function - c++ template argument in vector return value -




i have specific question can't find answer in questions regarding template member functions. want write function gets me data , return vector of specific type.

i have following:

#include <vector>  class testclass { public:     template <typename t> std::vector<t> getdata(int column); };   template <typename t> std::vector<t> testclass::getdata(int column){     std::vector<t> returndata;     return returndata; } 

and call function:

int main() {        testclass t;     std::vector<int> data = t.getdata(0);     return 0; } 

when compiling error:

../templatetest/main.cpp:9:31: error: no matching member function call 'getdata'     std::vector<int> data = t.getdata(0);                             ~~^~~~~~~ ../templatetest/testclass.h:8:42: note: candidate template ignored: couldn't infer template argument 't'     template <typename t> std::vector<t> getdata(int column);                                          ^ 

ok, can't template argument template in return type. fix try include template argument in call:

int main() {        testclass t;     std::vector<int> data = t.getdata<int>(0);     return 0; } 

this compiles gives me linker error:

undefined symbols architecture x86_64:   "std::__1::vector<int, std::__1::allocator<int> > testclass::getdata<int>(int)", referenced from:       _main in main.o 

one final try include template argument in function definition:

class testclass { public:     template <typename t> std::vector<t> getdata<t>(int column); }; 

this doesn't compile... :

../templatetest/testclass.h:8:42: error: member 'getdata' declared template     template <typename t> std::vector<t> getdata<t>(int column); 

is possible try do?

thanks!!

----------edit---------

putting implementation in header work. if prefer have implementation in .cpp. add the last line each implementation plan on using.

#include "testclass.h"  template <typename t> std::vector<t> testclass::getdata(int column){     std::vector<t> returndata;     return returndata; }  template std::vector<int> testclass::getdata(int column); 

i ran following program , compiles fine.

#include <vector>  class testclass { public:     template <typename t> std::vector<t> getdata(int column); };   template <typename t> std::vector<t> testclass::getdata(int column){     std::vector<t> returndata;     return returndata; }  int main() {     testclass t;     std::vector<int> data = t.getdata<int>(0);     return 0; } 

with templates should not using separate cpp file functions. make sure put definition in header file. necessary because type can passed in template , compiler need make new version of function each different type. when dealing templates define function inside class.

class testclass { public:     template <typename t> std::vector<t> getdata(int column){         std::vector<t> returndata;         return returndata;     } }; 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -