-1
class myclass {
   // Definitions of cotrs and dtor...
   // ...
   // Defining a method of mine that needs a template
   template < typename T >
   void dosome(T& par);
}

What to do in implementation in cpp file? I thought ti was good this:

template <typename T>
void myclass::dosome< T >(T& par) {
   // My code
}

But compiler gets really mad... What's the syntax in this context? Thankyou

Andry
  • 16,172
  • 27
  • 138
  • 246

2 Answers2

4

You want the entire template definition in the header.

Mud
  • 28,277
  • 11
  • 59
  • 92
  • No, I do not want the template to be applied to the entire class, just to the method. – Andry Nov 25 '10 at 21:58
  • Ok I could manage it, it was almost correct, what told me J. Calleja is a better way to do it but the compiler finally accepted both, there was a bug somewhere else.. :) – Andry Nov 25 '10 at 22:09
  • "what told me J. Calleja is a better way to do" No, Calleja told you to put it in the header, too. If you put the definition of a template method in a CPP file, it will *only be available* in that translation unit. In other words, you won't be able to use that template in other CPP files. – Mud Nov 26 '10 at 02:34
1

The syntax is the one used for functions:

template<typename T> void myclass::dosome(T &par) {
  // ...
}

However, normally you should include template definitinos in the header.

J. Calleja
  • 4,855
  • 2
  • 33
  • 54
  • Yes, it is correct, I tried a moment later... Thank you. Ah, one question, what do you mean by: "put it in the header"? – Andry Nov 25 '10 at 22:09
  • @Andry - see http://stackoverflow.com/questions/4111932/where-to-put-a-member-function-template – beldaz Nov 25 '10 at 23:36