Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling template function within template class

Tags:

c++

templates

Disclaimer: The following question probably is so easy that I might be shocked seeing the first answer. Furthermore, I want to apologize for any duplicate questions - syntactic problems are not always easy to identify be verbal explanation and thus searching for them is not as easy...

But enough of that. I have a two templated classes, one of those has a templated member function, the other class attempts to call that function. A minimal, error producing example is shown below:

#include <iostream>  template <typename T> class Foo { public:     Foo() {     }      template <typename outtype>     inline outtype bar(int i, int j, int k = 1) {         return k;     }  };  template <typename T> class Wrapper { public:     Wrapper() {     }      double returnValue() {         Foo<T> obj;         return obj.bar<double>(1,2); // This line is faulty.     }  };  int main() {     Wrapper<char> wr;     double test = wr.returnValue();     std::cout << test << std::endl;     return 0; } 

At compile time, this results in

expected primary-expression before 'double' expected ';' before 'double' expected unqualified-id before '>' token 

where all error messages are directed at the linke marked in the code.

I allready thank you for your ideas, no matter how obvious they are.

like image 376
Thilo Avatar asked Sep 13 '11 06:09

Thilo


People also ask

How do you call a function in a template?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.

Can a non template class have a template member function?

A non-template class can have template member functions, if required. Notice the syntax. Unlike a member function for a template class, a template member function is just like a free template function but scoped to its containing class.

Can a template class have virtual functions?

A class template can indeed contain virtual or pure virtual functions. This was employed by Andrei Alexandresu in "Modern C++ Design" to implement the visitor pattern using templates and type lists.

Which function is useful when template of template is used?

Explanation: As a template feature allows you to write generic programs. therefore a template function works with any type of data whereas normal function works with the specific types mentioned while writing a program.


1 Answers

obj.bar<double>(1,2); // This line is faulty. 

The template keyword is required here, as obj is an instance of a type Foo<T> which depends on the template parameter T, and so the above should be written as:

obj.template bar<double>(1,2); //This line is corrected :-) 

Read @Johannes's answer here for detail explanation:

  • Where and why do I have to put the "template" and "typename" keywords?
like image 149
Nawaz Avatar answered Oct 14 '22 23:10

Nawaz