0

I'm trying to access to a templated method from a vector iterator but I can't compile my code and I get through some error.

Here a sample of my code (without constructors, destructor, and all attributes and methods). However this code snippet reproduce the error I get.

#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;
};

Compilation error is the following one:

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));

I have no idea of what happens here.

Have you any idea of what I'm doing wrong?

Edit: Not exactly a duplicate of How to call a template member function?. However the answer given here https://stackoverflow.com/a/613132/2351966 is quiet interesting and answer also my question as Mattia F. as pointed out there was a template keyword missing.

Community
  • 1
  • 1
Elendil
  • 120
  • 1
  • 7

1 Answers1

1

Add the keyword template at line 47:

data.push_back(src->template getValue<dataT>(attr_index));

Otherwise it could be parsed as a comparison operation, like:

(src->getValue < dataT) > (attr_index)
Mattia F.
  • 1,720
  • 11
  • 21
  • Thanks this works fine. I'm not sure to understand why the code is parsed like `(src->getValue < dataT) > (attr_index)` whitout the keyword `template`. – Elendil Jul 05 '16 at 09:04