1

Good evening, I am trying to make a templated class which will have two members that are const_iterators point to the beginning and end of a subsection of a vector. I wanted to make the class generic so that I can use it for a vector of any type.

My class definitions is in file ThreadWorker.h and looks as follows:

template <typename T>
class VectorWorker
{
public:
    VectorWorker<T>() = default;
    VectorWorker<T>( std::vector<typename T>::const_iterator begin,  std::vector<typename T>::const_iterator end);
    void Work() const;

private:
    std::vector<typename T>::const_iterator beginIt; /* Stores value of left iterator that defines data for this worker */
    std::vector<typename T>::const_iterator endIt; /* Stores value of right iterator that defines data for this worker */
};

When i attempt to compile my code I get an error stating that the template arguments are invalid:

error: template argument 1 is invalid
     VectorWorker<T>( std::vector<typename T>::const_iterator begin,  std::vector<typename T>::const_iterator end);

I don't quite understand why I am getting this error, since I have specified via the "typename" keyword that T is a typename template parameter. Could anyone help explain what is going on here, or point me to a good resource? Thanks.

  • Shouldn't that be `typename std::vector::const_iterator` (pretty much everywhere in this code, btw). An excellent Q+A about both `typename` and `template` usage [can be found on this site](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords). – WhozCraig Mar 04 '18 at 00:54
  • Ok that looks like it solved the error. Although, I do not understand why. – Filip Gajowniczek Mar 04 '18 at 01:01
  • That was the reason for the followup link I provided, which discusses in detail where and how to use `typename`. Click the link in the last sentence ([or here](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords())). – WhozCraig Mar 04 '18 at 01:02
  • Possible duplicate of [Where and why do I have to put the "template" and "typename" keywords?](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Passer By Mar 04 '18 at 01:31

1 Answers1

1

Try moving the typename outside of the template argument list:

template<typename T>
class VectorWorker {
private:
    typename std::vector<T>::const_iterator beginIt;
    typename std::vector<T>::const_iterator endIt;
public:
    VectorWorker<T>() = default;
    VectorWorker<T>( typename std::vector<T>::const_iterator begin, typename std::vector<T>::const_iterator end ) :
        beginIt( begin ),
        endIt( end ) 
    {}    
};

What you previously had as both members and Constructor Parameters

std::vector<typename T>::const_iterator beginIt; 
std::vector<typename T>::const_iterator endIt;

std::vector<typename T>::const_iterator begin;
std::vector<typename T>::const_iterator end;

Are not Types

To resolve this the template parameter list for the members as well as the constructor parameters only needs the template type <T>

To make the const_iterators a type you need to declare them as a typename

typename std::vector<T>::const_iterator name;
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59