Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Default arguments cannot be added to an out-of-line definition of a member of a class template

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_;
}
like image 208
Ten Go Avatar asked Sep 03 '14 14:09

Ten Go


People also ask

What are the default arguments for a non-template member function?

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.

Where are default arguments allowed in C++?

// 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 .

When are non static class members not allowed in default 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.

How do default arguments work with using-declarations?

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:


1 Answers

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)
    {

    }
};
like image 76
Piotr Skotnicki Avatar answered Nov 15 '22 18:11

Piotr Skotnicki