类模板成员函数默认值问题:an out-of-line definition of a member of a class template cannot have default arguments
template <typename T> class A { void fun(int a = 0); }; template<typename T> void A<T>::fun(int a = 0) { /* */ }
对于类似上文代码,VS编译器会报 “an out-of-line definition of a member of a class template cannot have default arguments”错误。
其原因在于:带有默认参数值的模板类成员不能在类外进行定义,修改方式有两种。
第一种在类内进行定义:
template <typename T> class A { void fun(int a = 0){ /* */ } };
第二种在类外进行定义时,将默认值去掉:
template <typename T> class A { void fun(int a = 0); }; template<typename T> void A<T>::fun(int a /*= 0*/) { /* */ }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步