1

I'm trying to use the code below for my node editor, I'm using Visual Studio 2019, it errors out whenever I try to insert a node object

edit: forgot that push_back is the correct function, my bad

std::vector<Node*> nodes;

void Example(){
    Node* s = new Node();
    nodes.insert(s);
}

the full error:

no instance of overloaded function "std::vector&lt;_Ty, _Alloc>::insert 
[with _Ty=Node *, _Alloc=std::allocator&lt;Node *>]" matches the argument list
  • 3
    [A decent `std::vector::insert` reference](https://en.cppreference.com/w/cpp/container/vector/insert) will be very helpful here. Perhaps [some decent books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) would also be helpful? – Some programmer dude Jul 02 '20 at 19:52
  • 4
    Can you please explain which overload of https://en.cppreference.com/w/cpp/container/vector/insert you want to call with one argument? I don't see an overload which only takes one parameter. – Werner Henze Jul 02 '20 at 19:52
  • Please post a [mcve]. There's not enough detail / info in this question to provide an answer. – Jesper Juhl Jul 02 '20 at 19:52
  • 1
    `vector`'s `insert` methods all take multiple parameters, usually the item to add and the position where the item is to be added. Perhaps you are looking for `push_back`. – user4581301 Jul 02 '20 at 19:53
  • 1
    @Someprogrammerdude Must need more coffee. The syntax highlighting made me think it was calling out the argument "list", not the "argument list". – tadman Jul 02 '20 at 19:54

2 Answers2

3

insert needs an iterator argument for the position where you want to insert an item. E.g.:

nodes.insert(nodes.begin(), s);

If you don't want to specify the position and just want to append an element to the vector, you could use push_back:

nodes.push_back(s);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

std::vector<T,Allocator>::insert Inserts elements at the specified location in the container-

iterator insert( iterator pos, const T& value );

You are not passing an iterator to the position where you want the element to be inserted and hence the error. You could either pass an iterator or use push_back() if you want to insert the new element at the end of vector.

So the following would work -

nodes.insert(nodes.begin(), s);

OR

nodes.insert(nodes.end(),s);

Or you could use push_back() altogether -

nodes.push_back(s);
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32