【问题标题】:std::vector no instance of overloaded function matches the argument liststd::vector 没有重载函数的实例与参数列表匹配
【发布时间】:2020-07-02 19:49:44
【问题描述】:

我正在尝试将以下代码用于我的节点编辑器,我正在使用 Visual Studio 2019,每当我尝试插入节点对象时都会出错

编辑:忘了 push_back 是正确的函数,我的错

std::vector<Node*> nodes;

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

完整的错误:

no instance of overloaded function "std::vector&lt;_Ty, _Alloc>::insert 
[with _Ty=Node *, _Alloc=std::allocator&lt;Node *>]" matches the argument list

【问题讨论】:

标签: c++ vector visual-c++ compiler-errors


【解决方案1】:

insert 需要一个 iterator 参数来表示要插入项目的位置。例如:

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

如果您不想指定位置而只想将元素附加到向量,您可以使用push_back:

nodes.push_back(s);

【讨论】:

  • 是的,那是我的错,我把 insert 和 push_back 搞混了 :(
【解决方案2】:

std::vector&lt;T,Allocator&gt;::insert 在指定位置插入元素 容器中的位置-

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

您没有将迭代器传递到您希望插入元素的位置,从而导致错误。如果你想在向量的末尾插入新元素,你可以传递一个迭代器或使用push_back()

所以下面会起作用 -

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

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

或者你可以完全使用push_back() -

nodes.push_back(s);

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多