I have an iOS app that makes use of a C++ class from MPL called 'square.h' and whenever I build the app Xcode throws this error -
Default arguments cannot be added to an out-of-line definition of a member of a class template
Now, my understanding of C++ syntax is very little, so what does this error mean?
The problematic line is below -
template <class T>
TSquare<T>::TSquare (T size_, T x_, T y_, T scale_=0)
{
size = size_;
x = x_;
y = y_;
scale = scale_;
}
For a member function of a non-template class, the default arguments are allowed on the out-of-class definition, and are combined with the default arguments provided by the declaration inside the class body. If these out-of-class defaults would turn a member function into a default, copy, or move constructor the program is ill-formed.
// OK Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations. Template parameter lists use similar syntax for their default template arguments .
Non-static class members are not allowed in default arguments (even if they are not evaluated), except when used to form a pointer-to-member or in a member access expression: A default argument is evaluated each time the function is called with no argument for the corresponding parameter.
The using-declaration carries over the set of known default arguments, and if more arguments are added later to the function's namespace, those defaults are also visible anywhere the using-declaration is visible:
Default arguments are not part of the definition, only 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)
{
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With