views:

87

answers:

1

I am getting an error when I try to insert an item into a list (in C++). The error is that there is no matching function for call to the insert(). I also tried push_front() but got the same error.

Here is the error message:

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>]

Here is the code:

#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);
}
+3  A: 

insert needs both a position and an element - did you mean push_front or push_back?

RichieHindle
I also tried push_front() and got the same error.
Josh Curren
hmm... I just tried it again and push_front() works fine now.
Josh Curren
@Josh: you probably got the same error from line 44 (`sp.sales.insert( sale );`). Make sure you change both to use `push_back`.
outis
Ok... Thanks!!!
Josh Curren