Template function giving compilation error
Possible Duplicate:
std::map::const_iterator template compilation error
The idea is to create a function which takes the container type as a Template parameter. Since maps have to summed up differently compared to other sequential containers I overloaded the Sum function for the map as shown below.
This is the function which is giving me errors:
template<typename T1, typename T2>
double Sum(const map<T1,T2>& input)
{
double finalSum=0;
map<T1,T2>::const_iterator iter_begin=input.begin();
map<T1,T2>::const_iterator iter_end=input.end();
for(iter_begin; iter_begin!=iter_end; ++iter_begin)
{
finalSum=finalSum+(iter_begin)->second;
}
return finalSum;
}
Error:
1>c:documents and settingskeepmy documentsvisual studio 2010projectslevel 7exercise 2exercise 2sum.h(34): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:documents and settingskeepmy documentsvisual studio 2010projectslevel 7exercise 2exercise 2sum.h(34): error C2143: syntax error : missing ',' before '<'
This function is a part of the header file. My header file includes the function definitions also.
Few things: 1. I tried with typename but I might have been wrong. Templates are not my strong area yet. Feel free to point if typename is needed somewhere. Inline keyword will help?
Thanks in advance.
EDIT: Posting the whole Header file.With typename addition as suggested . But same error. The first version of Sum is compiling fine without typename. The second one overloaded for map is giving issues.
#ifndef SUM_H
#define SUM_H
template<typename T>
double Sum(const T& input)
{
double finalSum=0;
T::const_iterator iter_begin=input.begin();
T::const_iterator iter_end=input.end();
for(iter_begin; iter_begin!=iter_end; ++iter_begin)
{
finalSum=finalSum+(*iter_begin);
}
return finalSum;
}
//Mysterion !!!!!
template<typename T1, typename T2>
double Sum(const map<T1,T2>& input)
{
double finalSum=0;
typename map<T1,T2>::const_iterator iter_begin=input.begin();
typename map<T1,T2>::const_iterator iter_end=input.end();
for(iter_begin; iter_begin!=iter_end; ++iter_begin)
{
finalSum=finalSum+(iter_begin)->second;
}
return finalSum;
}
#endif
The error is coming at: double Sum(const map& input)
这是你需要typename
s的地方:
typename map<T1,T2>::const_iterator iter_begin=input.begin();
typename map<T1,T2>::const_iterator iter_end=input.end();
You've guessed it - you need typename
. Roughly, the rule is that if you're referring to a type using ::
and the thing on the left of ::
depends on template arguments, you must use typename
. So in your case:
typename map<T1,T2>::const_iterator iter_begin=input.begin();
typename map<T1,T2>::const_iterator iter_end=input.end();
You forgot to #include <map>
and you should also qualify its name with the std
qualifier since that is where map
lives.
Don't worry too much about the typename
(though it would be good to add them) because VC++ is lenient (and non-conformant) so it's not the primary error.
下一篇: 模板函数给出编译错误