c++ - Templated function in untemplated class -


i want create templated function in class not templated.

class theclass {     template<typename t>     int dosomething(t thing); }  template<typename t> int theclass::dosomething(t thing) {     .... } 

i don't know why getting error "member not found" when declare outside of class.

in c++ classes default member variables , methods private unless given "public:" keyword. if you're trying declare/initialize theclass , use dosomething method outside of class need make accessible. making public simplest way make method accessible, there other ways. missing semicolon @ end of class definition mentioned in comments. else looks fine.

here's working example.

#include <iostream>    class theclass  {    public:      template<typename t>      int dosomething(t thing);  };    template<typename t>  int theclass::dosomething(t thing)  {    std::cout << "dosomething(t thing)\n";    return 0;  }    int main() {    theclass x;    return x.dosomething(5);  }    running program console:  $ g++ class.c++ -o class  $ ./class    dosomething(t thing)


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -