Search code examples
c++templatescompiler-errorsdefault-parametersfunction-template

C++ error: default template arguments may not be used in function template


I am trying to understand what the value = T() means and how to fix it. Also the function is a constructor for a class.

template<typename T>
Accumulator<T>::Accumulator(const T& value = T())
{
     total = value;
}

This does not compile the following errors are:

error: default argument given for parameter 1 of `Accumulator<T>::Accumulator(const T&)'
error: after previous specification in `Accumulator<T>::Accumulator(const T&)'

Basically the function is a constructor for a class with a default argument that sets "total" a private variable of my class to "value" if given a value for an argument.


Solution

  • You should only specify the default parameter in the function declaration, in the header.