I've written the following minimal working example
#include <iostream>
template <typename T>
struct A {
enum myenum {faa, fee, fii};
myenum m_m;
A(const myenum& m = faa): m_m(m) {}
};
int main() {
A<int> a1;
A<int> a2(A<int>::fii);
std::cout << a1.m_m << std::endl
<< a2.m_m << std::endl;
return 0;
}
Compiling it and executing it results in
$ g++ main.cpp && ./a.out
0
2
However, when the definition of the function is written outside of the class (that is, as shown below)
#include <iostream>
template <typename T>
struct A {
enum myenum {faa, fee, fii};
myenum m_m;
A(const myenum& m);
};
template <typename T>
A<T>::A(const myenum& m = A<T>::faa): m_m(m) {}
int main() {
A<int> a2(A<int>::fii);
std::cout << a2.m_m << std::endl;
return 0;
}
I get the following compilation error
$ g++ main.cpp && ./a.out
main.cpp:11:1: error: redeclaration of ‘A<T>::A(const A<T>::myenum&)’ may not have default arguments [-fpermissive]
11 | A<T>::A(const myenum& m = A<T>::faa): m_m(m) {}
| ^~~~
How can I get rid of this compilation error?
You have to specify the default argument in declaration, and remove it from the definition.
For member functions of class templates, all defaults must be provided in the initial declaration of the member function.
template <typename T>
struct A {
enum myenum {faa, fee, fii};
myenum m_m;
A(const myenum& m = faa);
};
template <typename T>
A<T>::A(const myenum& m): m_m(m) {}