0

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;
	}
}
2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by coolbeanbob
Featured Replies
  • 1
    Narue 5,707   5 Years Ago

    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: [code] template <typename InputIterator> void printList(InputIterator first, InputIterator last) { while (first != last) cout << *it++; } [/code] … Read More

0

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

0

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?

1

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());
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.