我希望编写一个可以接受 2 或 3 个类型名的模板化结构。但是,程序会产生错误,template<class F1, class F2, class F3>...' cannot be overloaded
. 如何纠正?
template< typename F1, typename F2, typename F3> // this shouldn't be right because the compiler expects three typenames, and the program can provide two
struct Force{
F1 force1;
F2 force2;
F3 force3;
Force(F1 f1, F2 f2) : force1(f1), force2(f2) { // construct out of two forces
}
Force(F1 f1, F2 f2, F3 f3) : force1(f1), force2(f2), force3(f3) { // construct out of three forces
}
Point operator()(double t) {
return force1(t) + force2(t);
}
Point operator()(double t) { // this overloading should not be right because it has the same signature as above
return force1(t) + force2(t) + force3(t);
}
};
// this is used by the main program
template< typename F1, typename F2>
Force<F1, F2> make_physics(F1 first, F2 second){
return Force<F1, F2>(first, second);
}
// this is used by the main program
template< typename F1, typename F2, typename F3>
Force<F1, F2, F3> make_physics(F1 first, F2 second, F3 third){
return Force<F1, F2, F3>(first, second, third);
}