vector<double>. But there are several errors:
sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no
type
sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
':
sumtemplate.cpp:7: warning: `std::vector<T, std::allocator<_CharT>
::iterator'
is implicitly a typename
sumtemplate.cpp:7: warning: implicit typename is deprecated, please
see the
documentation for details
sumtemplate.cpp: In function `int sum(std::vector<T,
std::allocator<_CharT> >&)
[with T = double]':
sumtemplate.cpp:27: instantiated from here
sumtemplate.cpp:11: warning: return to `int' from `double'
sumtemplate.cpp:11: warning: argument to `int' from `double'
Please help me.
#include<iostream>
#include<vector>
using namespace std;
template <class T> sum(vector<T> &v){
vector<T>::iterator iter;
T result;
for(iter=v.begin();iter<v.end();iter++)
result=result+*iter;
return result;
}
int main(){
vector<int> v1;
vector<double> v2;
int sum1;
double sum2;
v1.push_back(1);
v1.push_back(2);
sum1=sum(v1);
cout<<"The result(int) is: "<<sum1<<endl;
v2.push_back(1.1);
v2.push_back(2.2);
sum2=sum(v2);
cout<<"The result(double) is: "<<sum2<<endl;
return 0;
}