Wir*_*uce 2 c++ templates stl g++ function-templates
我编写了以下模板函数来汇总std :: vector对象的内容.它本身就是一个名为sum.cpp的文件.
#include <vector>
template<typename T>
T sum(const std::vector<T>* objs) {
T total;
std::vector<T>::size_type i;
for(i = 0; i < objs->size(); i++) {
total += (*objs)[i];
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译此函数时,G ++会发出以下错误:
sum.cpp: In function ‘T sum(const std::vector<T, std::allocator<_Tp1> >*)’:
sum.cpp:6: error: expected ‘;’ before ‘i’
sum.cpp:7: error: ‘i’ was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
据我所知,返回此错误的原因是因为std::vector<T>::size_type无法解析为某种类型.这是我唯一的选择std::size_t(如果我理解正确但通常但并非总是相同std::vector<T>::size_type),或者是否有解决方法?
typename std::vector<T>::size_type i;
Run Code Online (Sandbox Code Playgroud)
http://womble.decadent.org.uk/c++/template-faq.html#disambiguation
| 归档时间: |
|
| 查看次数: |
445 次 |
| 最近记录: |