1

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.

Natalo77
  • 155
  • 7
  • It isn't a good idea to derive from `std::vector`. Your question is also showing all the earmarks of an [XY problem](http://xyproblem.info/). – PaulMcKenzie Apr 18 '19 at 17:16
  • I'm attempting to provide a wrapper for a vector that provides a few extension methods. Checking if an element exists within the vector, and removing an element, based on value. Is it a better idea to have this vector as a member variable of the List class I am creating? – Natalo77 Apr 18 '19 at 17:29
  • Before doing this, [please read this](https://stackoverflow.com/questions/4353203/thou-shalt-not-inherit-from-stdvector). The vector should be a member. Second, why are you *not* using the algorithm functions such as `std::find` and use `auto` to simplify things? Why create busy work for yourself when there is no need to? – PaulMcKenzie Apr 18 '19 at 17:31
  • Why are you trying to return `nullptr` as if it were a `std::vector` iterator? They are sometimes pointers (depending on the implementation), but rarely null. – Davis Herring Apr 19 '19 at 20:45
  • @DavisHerring It was a decision scoped only within the class that nullptr from that method would represent an empty list/unfound element. std::find is better and I've since moved onto that. I've answered this question with the solution that I found that caused this problem/question. – Natalo77 Apr 19 '19 at 21:04
  • @Natalo77: But you shouldn’t assume that `nullptr` is usable as a `vector` iterator—even as a “not found” value. – Davis Herring Apr 19 '19 at 21:18
  • @DavisHerring Other methods would check for the variable being returned into for nullptr before using. However, I understand the issues the point you raise causes, but have since moved onto `std::find` and this wasn't causing the error in the question. – Natalo77 Apr 19 '19 at 21:29
  • @Natalo77: But it might be a type error (*i.e.*, not compile) to use that value even long enough to check it! – Davis Herring Apr 19 '19 at 21:32
  • @DavisHerring Ah. A second look may have spotted a mistake in my original post. Whilst faffing about to try and resolve my error, I must have removed the `*` from the return value of `std::vector::iterator` – Natalo77 Apr 21 '19 at 17:43
  • @Natalo77: But then you can’t return `iter` (except by some thoroughly clumsy method like making it `static` or usung `new` to keep (a copy of) it alive). – Davis Herring Apr 21 '19 at 17:54
  • @DavisHerring I know, my implementation was dogshite xD – Natalo77 Apr 24 '19 at 09:33

1 Answers1

-1

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.

Natalo77
  • 155
  • 7