c++模板使用的异常,求帮忙
c++模板使用的错误,求帮忙。
当定义如12和13行时,报错如下:
findMax.cpp:38: instantiated from here
findMax.cpp:13: error: dependent-name 'std::vector::size_type' is parsed as a non-type, but instantiation yields a type
findMax.cpp:13: note: say 'typename std::vector::size_type' if a type is meant
------解决方案--------------------
有个名词叫 模板参数依赖
有个C++关键字叫typename
自己google吧
9 template <typename ob, typename comparator>
10 const ob& findMax(const std::vector<ob> &arr, comparator cmp)
11 {
12 //std::vector<ob>::const_iterator maxIn = arr.begin(); ---这里的定义有问题
13 //std::vector<ob>::size_type maxIn = 0; ----这里的定义有问题
14 int maxIn = 0;
15 //for (std::vector<ob>::const_iterator it = arr.begin(); it != arr.end(); ++it)
16 //for (std::vector<ob>size_type st = 0; st != arr.size(); ++st)
17 for (int st = 0; st != arr.size(); ++st)
18 {
19 //if (cmp.isLessThan(*maxIn, *it))
20 if (cmp.isLessThan(arr[maxIn], arr[st]))
21 maxIn = st;
22 }
23
24 return arr[maxIn];
25 }
当定义如12和13行时,报错如下:
findMax.cpp:38: instantiated from here
findMax.cpp:13: error: dependent-name 'std::vector::size_type' is parsed as a non-type, but instantiation yields a type
findMax.cpp:13: note: say 'typename std::vector::size_type' if a type is meant
------解决方案--------------------
有个名词叫 模板参数依赖
有个C++关键字叫typename
自己google吧