2010-04-10 35 views
1

我嘗試向列表中插入項目時出現錯誤(使用C++)。錯誤是沒有用於調用insert()的匹配函數。我也試過push_front(),但得到了同樣的錯誤。無匹配函數錯誤,用於在C++中插入列表

以下是錯誤消息:

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); 
} 
+1

「.cpp」文件不應包括在內,它們應該單獨編譯並鏈接到最終的二進制文件中。 – outis 2010-04-10 23:43:14

回答

4

insert需求既是一個位置和元素 - 你的意思是push_frontpush_back

+0

我也嘗試push_front()並得到相同的錯誤。 – 2010-04-10 23:41:02

+0

嗯...我只是試了一遍,push_front()現在工作正常。 – 2010-04-10 23:44:01

+2

@Josh:你可能從第44行('sp.sales.insert(sale);')得到同樣的錯誤。確保你改變了使用'push_back'。 – outis 2010-04-10 23:44:25

相關問題