大虾们帮我看看关于模板的问题!!!
我的目的就是用模板写一个不同类型的求和函数,怎么老是提示错误阿!!!大虾们快来救命啊!!!!
代码如下:
程序代码:
#include <iostream> using namespace std; template <class eletype> //这里把class换成typename也不对 eletype sum(eletype param1, eletype param2); int main(void) { int ia = 1, ib = 1, ic; float fa = 1.0f, fb = 1.0f, fc; double da = 1.0, db = 1.0, dc; ic = sum(ia, ib); fc = sum(fa, fb); dc = sum(da, db); cout<<"ia+ib="<<ic<<endl; cout<<"fa+fb="<<fc<<endl; cout<<"da+db="<<dc<<endl; return 0; } eletype sum(eletype param1, eletype param2) //这里提示说“‘eletype’does not name a type” { return param1+param2; }