Reputation: 291
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.
Upvotes: 1
Views: 203
Reputation: 7905
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_iterator
s a type you need to declare them as a typename
typename std::vector<T>::const_iterator name;
Upvotes: 1