How to define a template method for a class without a template?

My compiler is unhappy with the way I implement my template methods. It gives tons of "undefined type T" type error messages for these implementations.

This is my first method, it is implemented outside the class block:

class VectorConvertor { public: // ... template <class T> static void ReverseVectorElements(std::vector<T> & Vector); // ... }; template <class T> void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector) { std::vector<T>::size_type size = Vector.size(); T swap; for (std::vector<T>::size_type i=0; i<size/2; i++) { swap = Vector.at(i); Vector.at(i) = Vector.at(size-1-i); Vector.at(size-1-i) = swap; } } 

Another is that; This time the method is implemented inside the class:

 class ElementaryMath { public: // ... template <class T> static char sign(T num) { return (num >= static_cast<T>(0)) ? static_cast<char>(+1) : static_cast<char>(-1); } // ... } 

Is there something wrong with my code, or is it just a problem with the compiler itself?

IDE and compiler: Visual Studio 2010

+4
source share
5 answers

Your code looks good to me. But there is one thing that has arisen in my head. Can you check if the sign function has been defined before? Just hover over it. C runtime library implements some of its functions using the #define keyword, and because of this, you cannot subsequently define a function with the same name.

+5
source

You are missing typename and semicolons, but otherwise the code looks OK. IMHO it's time to file an error message if it still doesn't work.

By the way, swap code is best done using std::swap .

+4
source

Your code compiles without errors on VS2005 (except for the missing semicolon at the end of the ElementaryMath definition), so you can look for a compiler error.

VS2010 SP1 is available in beta here . It may help, but obviously its beta ...

+1
source

here:

 template <class T> void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector) { typename std::vector<T>::size_type size = Vector.size(); T swap; for (typename std::vector<T>::size_type i=0; i<size/2; i++) { swap = Vector.at(i); Vector.at(i) = Vector.at(size-1-i); Vector.at(size-1-i) = swap; } } 
+1
source
 class VectorConvertor { public: // ... template <typename T> static void ReverseVectorElements(std::vector<T> & Vector); }; template <typename T> void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector) { std::vector<T>::size_type size = Vector.size(); T swap; for (std::vector<T>::size_type i=0; i<size/2; i++) { swap = Vector.at(i); Vector.at(i) = Vector.at(size-1-i); Vector.at(size-1-i) = swap; } } int main() { std::vector <int> i(10,0); VectorConvertor obj; // Since your class isn't a template, template parameter // isn't required for a class template instantiation. // However, if your class was a template class, template- // parameter must have been required for a class template // instantiation. obj.ReverseVectorElements(i); // Equal to obj.ReverseVectorElements<int>(i); // Based on type of i, compiler can instantiate a // template function by default. getchar(); return 0; } 

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1335978/


All Articles