2

Possible Duplicate:
How to compile Templates located in different files in C++?

I want to make a class template with a member function template.

If I code it inline (where I declare it) it works fine, but if I want to code it outline (like the example bellow) I get a error that says "'Example ::func' : unable to match function definition to an existing declaration"

template <typename T>
class Example {
public:

    template <typename F>
    void func(F &f);

};


template <typename T, typename F>
void Example<T>::func(F &f) {
    //My code
}
Community
  • 1
  • 1
Nactive
  • 540
  • 1
  • 7
  • 17
  • @Joe Thanks for the link, I've been looking for a good one to use in dupe closing, but I'm not sure that is the issue here. –  May 09 '11 at 19:01
  • The syntax for declaring templates inside templates in various permutations is inscrutable and nonsensical. As near as I can tell, it's a mass of special cases with no hard and fast rules. – Omnifarious May 09 '11 at 19:06

4 Answers4

3

You may will want to make this function inline though, if it's in your header file. Otherwise you could get duplicate symbol errors.

template <typename T> template <typename F>
void Example<T>::func(F &f) {
    //My code
}
GWW
  • 43,129
  • 11
  • 115
  • 108
1

The correct format for defining template members of template classes is as follows:

template <typename T> template <typename F>
void Example<T>::func(F &f) {
//My code
}
EdF
  • 549
  • 3
  • 12
0

Take a look here. I just answered a question on explicit instantiation of templates. You should be able to find LOTS more questions on SO.

Community
  • 1
  • 1
Sriram
  • 10,298
  • 21
  • 83
  • 136
0
template <typename T>
template <typename F>
void Example<T>::func(F &f) {
    //My code
}

Example class has only one template parameter T.

Mahesh
  • 34,573
  • 20
  • 89
  • 115