【问题标题】:No Matching Function Error for inserting into a list in c++在 c++ 中插入列表时没有匹配函数错误
【发布时间】:2010-04-10 23:35:18
【问题描述】:

当我尝试将项目插入列表(在 C++ 中)时出现错误。错误是没有匹配的函数可以调用 insert()。我也试过 push_front() 但得到了同样的错误。

这是错误信息:

main.cpp:38: error: no matching function for call to ‘std::list<Salesperson, std::allocator<Salesperson> >::insert(Salesperson&)’
    /usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/list.tcc:99: note: candidates are: std::_List_iterator<_Tp> std::list<_Tp, _Alloc>::insert(std::_List_iterator<_Tp>, const _Tp&) [with _Tp = Salesperson, _Alloc = std::allocator<Salesperson>]
    /usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_list.h:961: note:                 void std::list<_Tp, _Alloc>::insert(std::_List_iterator<_Tp>, size_t, const _Tp&) [with _Tp = Salesperson, _Alloc = std::allocator<Salesperson>]

代码如下:

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include "Salesperson.h"
#include "Salesperson.cpp"
#include "OrderedList.h"
#include "OrderedList.cpp"
using namespace std;

int main(int argc, char** argv)
{
    cout << "\n------------ Asn 8 - Sales Report ------------" << endl;

    list<Salesperson> s;
    int id;
    string fName, lName;
    int numOfSales;
    string year;

    std::ifstream input("Sales.txt");
    while( !std::getline(input, year, ',').eof() )
    {
        input >> id;
        input >> lName;
        input >> fName;
        input >> numOfSales;
        Salesperson sp = Salesperson( id, fName, lName );
        s.insert( sp ); //THIS IS LINE 38 **************************
        //s.push_front( sp ); //ALSO GETS THE SAME ERROR


        for( int i = 0; i < numOfSales; i++ )
        {
            double sale;
            input >> sale;
            sp.sales.insert( sale );
        }
    }

    cout << endl;
    return (EXIT_SUCCESS);
}

【问题讨论】:

  • ".cpp" 文件不应包含在内,它们应单独编译并链接到最终的二进制文件中。

标签: c++ list insert


【解决方案1】:

insert 需要一个位置和一个元素 - 你的意思是push_front 还是push_back

【讨论】:

  • 嗯...我刚刚又试了一次,push_front() 现在可以正常工作了。
  • @Josh:您可能在第 44 行 (sp.sales.insert( sale );) 遇到了同样的错误。确保将两者都更改为使用push_back
猜你喜欢
  • 2012-12-23
  • 2016-03-02
  • 2016-01-18
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多