• Advertisement
Sign in to follow this  

Compilation errror in Derived STL Vector class

This topic is 2737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a derived class (derived from std::vector) and a member function insert as follows.
template <class Type> class MeshVector : public std::vector<Type>
{
public:
std::vector<Type>::iterator insert(const Type& t)
{
if (find(t)==end())
push_back(t);
return &back();
}
};


When I compiled using Visual studio Team System 2008, I am getting an error,cannot convert parameter 1 from 'Vertex **' to 'const std::_Vector_iterator. Please help me in fixing the error.

Share this post


Link to post
Share on other sites
Advertisement
back() gives reference to element, not iterator.
If you want iterator to last element, you must return end() - 1;

Share this post


Link to post
Share on other sites
Why would you want to derive from std::vector<>?

In case you want to optimize memory allocation, there is already a second template argument to vector that you have missed as it defaults,

template <class T, class Allocator=allocator<T> > class vector;


--> allocators

Share this post


Link to post
Share on other sites
My guess from the code shown in the OP is that you want something like a std::set or an unordered_set where elements are not permitted to appear more than once.

Make sure you select the right container for the job.


Also, don't derive publicly from the standard library containers; they weren't designed for that.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement