Hello,

I am getting an error when I try to pass a linked list and an iterator to the printList function below. For some reason it does not recognize the printList function call.

I get the following error on line 39.

no matching function for call to 'printList(std::_List_iterator<Event, std::allocator<Event> >&'

#include "utility.h"
#include "Item.h"
#include "Event.h"
using namespace std;


template< typename T > void printStack( T &stackRef );
template< typename T > void printQueue( T &queueRef );
template< typename T > void printList( T &it, T &list );



int main()
{

    Event planeChicago;
    Event planeDallas;
    planeChicago.makeEvent(6, "plane", "Chicago");
    planeDallas.makeEvent(5, "plane", "Dallas");


// List Testing ====================================================================================================

    list<Event> mylist;
	list<Event>::iterator current;  //pointer into the list

	mylist.push_back(planeChicago);  //insert value at front of list
	mylist.push_back(planeDallas);   //insert value at back of list

    cout << "the list contains: " << endl;
	for (current = mylist.begin(); current != mylist.end(); current++)
	{
		cout << *current;
	}

mylist.sort();


printList(current, mylist);                                 // error: no matching function for call to 'printList(std::_List_iterator<Event, std::allocator<Event> >&'

//    cout << "the list contains: " << endl;
//	for (current = mylist.begin(); current != mylist.end(); current++)
//	{
//		cout << *current;
//	}

// End List Testing ====================================================================================================




template< typename T > void printStack( T &stackRef )
{
    for (int j = 0; j < 12; j++)
    {
        cout << j << ") " << stackRef.top();    // Returns top element of stack
        stackRef.pop();                         // Removes top element from the stack
    }
}

template< typename T > void printQueue( T &queueRef )
{
    for (int j = 0; j < 12; j++)
    {
        cout << j << ") " << queueRef.front();    // Returns top element of stack
        queueRef.pop();
    }
}

template< typename T > void printList( T &it, T &list )
{
    cout << "the list contains: " << endl;
	for (it = list.begin(); it != list.end(); it++)
	{
		cout << *it;
	}
}

Recommended Answers

All 4 Replies

list<Event> and list<Event>::iterator are not the same type, but printList() expects a reference to T for both parameters.

list<Event> and list<Event>::iterator are not the same type, but printList() expects a reference to T for both parameters.

Ok. I am using bits and pieces from an example I found online. It looks like "list<Event>::iterator current;" gives a list pointer. How do I pass a pointer to a function?

You're approaching this the wrong way, the function needs to recognize both the list type and the iterator type. Or better yet, follow the generic pattern given by the standard algorithms:

template <typename InputIterator>
void printList(InputIterator first, InputIterator last)
{
    while (first != last)
        cout << *it++;
}
cout << "The list contains: ";
printList(mylist.begin(), mylist.end());

Thank you. You are right, that is more clever than the way I was trying to implement the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.