2

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.

Oktalist
  • 14,336
  • 3
  • 43
  • 63
John
  • 23
  • 5

1 Answers1

3

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

Laura Maftei
  • 1,863
  • 1
  • 15
  • 25
  • Okay I think i understand what you mean. The default arguments should be assigned in the earliest occurrence of a function definition. So removing the '= T()' should fix it but I get link errors now. – John Oct 18 '14 at 20:45
  • Yes, that's what i meant. – Laura Maftei Oct 19 '14 at 06:32