ganlaw
ganlaw

Reputation: 1313

Vector is not accepting insert as a function

At the line temp.insert(it); I keep getting the error No matching member function for call to insert, which means that at some point in my code temp became something other than a vector. anybody have any ideas?

#include <iostream>
#include <vector> 

using namespace std;

vector<int> removeDuplicates(vector<int> stuff)
{

vector<int> temp;
vector<int>::iterator it;

for(vector<int>::size_type i = 0; i < stuff.size(); i++){
    //iterator to find stuff
    it = find(stuff.begin(), stuff.end(), stuff);

    //if it not in temp then add it
    if (it == temp.end()) {
        temp.insert(it); //No matching member function for call to insert
    }
}

return temp;


}

int main(int argc, const char * argv[])
{


vector<int> values;
for(int i = 0; i < 5; i++){
    values.push_back(i);
    values.push_back(i+1);
}

removeDuplicates(values);
return 0;
}

well the answers helped me out a lot, but I decided to rewrite the code and here is what I got and it seems to work pretty well

#include <iostream>
#include <vector> 

using namespace std;

vector<int> removeDuplicates(vector<int> oldvector)
{

// newvector to hold non-duplicates
vector<int> newvector;

vector<int>::iterator it;



for(vector<int>::size_type i = 0; i < oldvector.size(); i ++){

    //If it find a value then it returns the first element
    it = find(newvector.begin(),newvector.end(), oldvector[i]);
    if(it == newvector.end()){
        newvector.push_back(oldvector[i]);
    }



}

return newvector;


}

int main(int argc, const char * argv[])
{


vector<int> values;
vector<int> newvalues;

for(int i = 0; i < 5; i++){
    values.push_back(i);
    values.push_back(i+1);
}



newvalues = removeDuplicates(values);

//Print out the vector without duplicates
for(vector<int>::size_type i = 0; i < newvalues.size(); i ++){
    cout << newvalues[i] << "\t";

}

return 0;
}

Thanks for the help with the original question, i learned a lot.

Upvotes: 5

Views: 18480

Answers (2)

Ed Swangren
Ed Swangren

Reputation: 124632

The three (pre-C++11) valid signatures for std::vector::insert are:

iterator insert ( iterator position, const T& x );
    void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
    void insert ( iterator position, InputIterator first, InputIterator last );

You chose invalid option #4:

temp.insert(iterator);

That option does not exist, you'll have to chose from one of the three listed above.


On a side note (as ildjarn pointed out), C++11 adds more... and takes a few away. Regardless, none of them would be valid the way you are calling it. See: http://en.cppreference.com/w/cpp/container/vector/insert

Upvotes: 6

Robᵩ
Robᵩ

Reputation: 168616

which means that at some point in my code temp became something other than a vector

No, that didn't happen. More to the point, it couldn't happen.

The following function does not exist. There is no matching function that takes just an iterator.

temp.insert(it); //No matching member function for call to insert

Try, for example,

temp.insert(it, 7); // will insert 7 immediately prior to end()

Upvotes: 5

Related Questions