【问题标题】:Vector iterator -> Printing clases (missing argument)矢量迭代器 -> 打印类(缺少参数)
【发布时间】:2015-11-21 11:25:41
【问题描述】:

我正在使用 VS V++ 2013。我的函数将向量作为参数并打印出类(我有抽象类和几个孩子)。除了印刷,我什么都做了。我有向量迭代器的问题。 每次我尝试编译时都会收到此错误 这是我的功能: 错误 1 ​​错误 C3867: 'std::vector>::begin': 函数调用缺少参数列表;使用 '&std::vector>::begin' 创建指向成员的指针

void printList(const vector <Employee *> & Ve)
{   
    for (std::vector < Employee *>::iterator it = Ve.begin ;it != Ve.end(); ++it)
    {
    }
}

【问题讨论】:

    标签: c++ vector iterator polymorphism


    【解决方案1】:

    你错过了括号:

    for (std::vector < Employee *>::const_iterator it = Ve.begin() ;it != Ve.end(); ++it)
                                                                ~~
    

    另一个问题,您将vector 传递为const,然后调用begin() 将返回const_iterator。您应该更改迭代器的声明,或者只使用auto(来自 c++11):

    for (auto it = Ve.begin() ;it != Ve.end(); ++it)
    

    【讨论】:

    • 当我这样尝试时,我得到了另一个错误 **错误 1 ​​错误 C2440: 'initializing' : cannot convert from 'std::_Vector_const_iterator<:_vector_val> >>' 到 'std::_Vector_iterator<:_vector_val>>>' **
    • 您可以使用const_iterator。请参阅我编辑的答案。
    • 我想通了,但是当我尝试使用我的类中的虚拟函数时,我无法使用。它说 IntelliSense: class "std::_Vector_const_iterator<:_vector_val>>>" has no member "printPay" it-&gt;printPay();
    • @NikolaButigan printPay 是非常量成员函数吗?向量 Ve 作为 const 传递,不能在其上调用非 const 成员函数。你能在没有 const 的情况下通过它吗?即void printList(vector &lt;Employee *&gt; &amp; Ve)
    • 循环现在很好(适用于const_iteratorauto,但我无法从类中访问我的printPay(); 虚函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2012-09-08
    • 2018-10-03
    • 2011-06-08
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多