简体   繁体   中英

How do I return a Generic Vector Iterator from an Inline Template method?

OK. As the question states I have an Inline Template method that returns an Iterator to a generic vector.

note: I know I should change the implementation to std::find, but that's not the problem I'm facing.

#include <vector>

template<typename T>
class List : public std::vector<T>
{
public:
    List();
    ~List();

    bool Contains(T object);

    void Remove(const T& object);

private:
    typedef typename std::vector<T>::iterator iterator;

    typename std::vector<T>::iterator IteratorOf(const T& object);
};


template<typename T>
inline typename std::vector<T>::iterator List<T>::IteratorOf(const T& 
object)
{
    if (this->empty())
        return nullptr;
    else
    {
        for (typename std::vector<T*>::iterator iter = this->begin();
          iter != this->end();                                       
          iter++)
        {
        if (*object == *iter)
            return iter;
        }
    }
    return nullptr;
}

This throws a myriad of compiler errors when I try and run it.

I seem to have tried everything however this method simply is not liked.

Beginning to wonder if there's a problem with returning generic iterators.

The actual answer to this myriad of compiler errors is one that is completely unrelated to this specific problem.

You cannot define templated methods outside of the header file

Visual Studio will throw a whole HEAP of errors, leading you down a rabbit hole away from what is a relatively simple issue to fix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM