C ++ 11 Using Iterators for Template Parameter Type Vectors

I have a template class that contains a vector of type pointers specified as a template parameter. I want to be able to use range-based iteration to iterate over a limited part of a vector. My class contains the following functions:

template< typename ObjectType >
class ObjectList
{

    ...

public:

    //! Begin iteration over a list of objects
    std::vector<ObjectType*>::iterator begin();

    //! Iterator to one past the end of the list of objects
    std::vector<ObjectType*>::iterator end();

private:

    std::vector<ObjectType*> object_ptrs;
};

This does not compile with the description Error C2061 syntax error: identifier 'iterator'. I can’t think of any reason why it std::vector<ObjectType>::iteratorwill never be found, unless ObjectTypeit can be found, but the rest of the class is successfully created if I delete the lines that refer to iterator.

Anyone have an idea what is going on? I am sure it is possible, I am missing something obvious. Thanks in advance!

+4
3

typename, std::vector<ObjectType*> - , ObjectType.

typename std::vector<ObjectType*>::iterator begin();
typename std::vector<ObjectType*>::iterator end();

$14.6/2 [temp.res]

, , , , , typename.

+1

, , :

template< typename ObjectType >
class ObjectList
{
public:

    //! Begin iteration over a list of objects
    typename std::vector<ObjectType*>::iterator begin();

    //! Iterator to one past the end of the list of objects
    typename std::vector<ObjectType*>::iterator end();

private:

    std::vector<ObjectType*> object_ptrs;
};
+1

, iterator . typename , :

typename std::vector<ObjectType*>::iterator begin();

In addition, the new g ++ (at least since 5.3.0) creates a more readable error message:

main.cpp: 11: 5: error: required typenamebefore std::vector<ObjectType*>::iterator, because std::vector<ObjectType*>is a dependent area      std::vector<ObjectType*>::iterator begin()

+1
source

Source: https://habr.com/ru/post/1625458/


All Articles