在vector迭代器上调用模板化方法
C++ call templated method on vector iterator
我试图访问一个模板化的方法从一个向量迭代器,但我不能编译我的代码,我通过一些错误。
下面是我的代码示例(没有构造函数、析构函数以及所有属性和方法)。然而,这个代码片段再现了我得到的错误。#include <vector>
#include <boost/any.hpp>
class Value: public boost::any {
public:
Value () :
boost::any() {
}
template<typename dataT>
Value (const dataT& value) :
boost::any(value) {
}
template<typename dataT>
dataT as () const {
return boost::any_cast<dataT>(*this);
}
};
class Src {
public:
inline const Value& operator[] (const int& index) const {
return _values[index];
}
inline Value& operator[] (const int& index) {
return _values[index];
}
template<typename dataT>
dataT getValue (const int& index) const {
return operator[](index).as<dataT>();
}
private:
std::vector<Value> _values;
};
template<typename SRC>
class A{
public:
template<typename dataT>
std::vector<dataT> getValues (const size_t& attr_index) const {
std::vector<dataT> data;
typename std::vector<dataT>::iterator src;
for (src = _data.begin(); src != _data.end(); ++src) {
data.push_back(src->getValue<dataT>(attr_index));
}
return data;
}
private:
std::vector<SRC> _data;
};
编译错误如下:
test.h: In member function ‘std::vector<dataT> A<SRC>::getValues(const size_t&) const’:
test.h:49:41: error: expected primary-expression before ‘>’ token
data.push_back(src->getValue<dataT>(attr_index));
我不知道这里发生了什么。
你知道我做错了什么吗?编辑:不完全重复如何调用模板成员函数?然而,这里给出的答案https://stackoverflow.com/a/613132/2351966非常有趣,也回答了我的问题,正如Mattia F. as指出的那样,缺少一个template
关键字。
在第47行添加关键字template
:
data.push_back(src->template getValue<dataT>(attr_index));
否则可以解析为比较操作,如:
(src->getValue < dataT) > (attr_index)
相关文章:
- 如何强制从重写方法调用重写的方法基方法?
- C++:使用方法调用析构函数的顺序是什么?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 指向类方法调用的指针
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 是否有可以处理方法调用依赖关系的设计模式?
- 如何缩短C++中的方法调用?
- 从部分专用模板方法调用模板非静态方法
- 有没有办法禁止派生类中的基类方法调用?
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 从父方法调用子方法
- 如何将子方法调用到父方法
- 虚拟函数在哪里使用 vpointer to vtable 来解析方法调用,非虚拟方法存储在哪里以及如何解析它们?
- 从静态方法调用静态函数指针
- 从同一类中的另一个方法调用方法时出错
- 方法调用意外地像 l 值一样起作用
- 无法从派生的一个方法调用基类方法
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- C 多线程JAVA JNI方法调用