C ++ - default arguments cannot be added to the external definition of a class template member

I have an iOS application that uses the C ++ class from MPL called "square.h", and whenever I create an Xcode application, it throws this error -

The default arguments cannot be added to the external definition of a class template member.

Now my understanding of C ++ syntax is very small, so what does this error mean?

The problem line below is

template <class T> TSquare<T>::TSquare (T size_, T x_, T y_, T scale_=0) { size = size_; x = x_; y = y_; scale = scale_; } 
+5
source share
2 answers

The default arguments are not part of the definition, only the declaration:

 template <class T> class TSquare { public: TSquare (T size_, T x_, T y_, T scale_ = 0); }; // out-of-line definition of a member of a class template: template <class T> TSquare<T>::TSquare (T size_, T x_, T y_, T scale_ /* = 0 */) { } 

or

 template <class T> class TSquare { public: // in-line definition of a member of a class template TSquare (T size_, T x_, T y_, T scale_ = 0) { } }; 
+10
source

According to standard ( N3797 ) ยง8.3.6 / 6 Default Arguments [dcl.fct.default] (emphasis mine)

With the exception of member functions of class templates, the default arguments in the definition of a member function that appear outside the class definition are added to the set of default arguments provided by the member Function declaration in the class definition. The default arguments for the member function of the class template must be specified in the initial declaration of the member function in the class template.

The above means that the default arguments for member functions of template classes go only inside the class definition. That way, you either put the declaration of the member function in the default argument:

 template<T> class TSquare { ... TSquare (T size_, T x_, T y_, T scale_ = 0); ... }; 

Or you should put the entire definition of a member function inside the class definition.

 template<T> class TSquare { ... TSquare (T size_, T x_, T y_, T scale_ = 0) { ... } ... }; 
+4
source

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


All Articles