Unable to overload existing std :: vector functions

I am implementing a POC implementation, and on demand I need to extend the std :: vector API, which will take only one parameter (the value that needs to be inserted), and internally the code will add this to the end of the container.

I created my own class (ValVector), derived from std :: vector, and defined a custom paste API that accepts a single parameter, but generates an error when compiling.

Rate the quick answer.

Below is the code for the fragment with the error message:

#include <iostream> #include <vector> using namespace std; typedef bool BOOL; template<class T, class Allocator = allocator<T>> class ValVector : public std::vector<T, Allocator> { public: BOOL insert(const T& elem) { return (this->insert(this->end(),elem)!=this->end()); } }; int main () { std::vector<int> myvector (3,100); std::vector<int>::iterator it; myvector.push_back (200 ); ValVector<int> mKeyAr; mKeyAr.insert(10); // std::cout << "myvector contains:"; for (auto it=mKeyAr.begin(); it<mKeyAr.end(); it++) std::cout << ' ' << *it; std::cout << '\n'; return 0; } 

Error message:

 In instantiation of 'BOOL ValVector<T, Allocator>::insert(const T&) [with T = int; Allocator = std::allocator<int>; BOOL = bool]': 23:19: required from here 11:72: error: no matching function for call to 'ValVector<int>::insert(std::vector<int>::iterator, const int&)' 11:72: note: candidate is: 11:10: note: BOOL ValVector<T, Allocator>::insert(const T&) [with T = int; Allocator = std::allocator<int>; BOOL = bool] 11:10: note: candidate expects 1 argument, 2 provided In member function 'BOOL ValVector<T, Allocator>::insert(const T&) [with T = int; Allocator = std::allocator<int>; BOOL = bool]': 11:88: warning: control reaches end of non-void function [-Wreturn-type] 
+5
source share
3 answers

To answer your real question: declaring a function in a class hides all inherited functions with the same name in this class. In other words, since ValVector has a function called insert , the inherited std::vector::insert no longer appears in it. Probably the best way to solve this problem is to bring the inherited insert back into scope with the using declaration:

 template<class T, class Allocator = allocator<T>> class ValVector : public std::vector<T, Allocator> { public: using std::vector<T, Allocator>::insert; BOOL insert(const T& elem) { return (this->insert(this->end(),elem)!=this->end()); } }; 

However, I have a comment. I think your approach is wrong. std containers are not intended for public inheritance; if nothing else, they have no virtual destructor and no protected members. You will be better off providing a free function that can then be used with any std::vector , and not just your type:

 template <class T, class A> BOOL insert(std::vector<T, A> &vec, const T &val) { return vec.insert(vec.end(), val) != vec.end(); } 

Or make it more versatile for working with any container:

 temlate <class C, class E> BOOL insert(C &cont, const E &val) { return cont.insert(cont.end(), val) != cont.end(); } 
+4
source

When you create your own member function for something that is not virtual, you hide all the same name functions above (I think this is called shading). Now you are trying to call a function that is no longer visible.

Is there a good reason not just to make a separate function that does what you need, or do you need to extract it from a vector? Nothing you do requires access to protected data or features ...

 // from vector.insert(data); // to insert_poc(vector, data); 
+1
source

Just change your insert as below to make it work

  BOOL insert(const T& elem) { return (std::vector<T>::insert(this->end(),elem)!=this->end()); } 

Greetings

0
source

All Articles