I am running into an issue trying to use an iterator to print. I am trying to print the widget name and widget type from a class and am getting an error on the iterator part. The rest of the code works fine but any comments on that are more than welcome. I marked in the code where the error is.

#include "widget.h"

#include <iostream>
#include <list>
using namespace std;

void showMenu();
void getInfo(widget&, string name, double type);



int main()
{
	string name;  //widget name
	double type;   //widget type


	//Create an empty widget list
	widget widgetList ("", 0);
	list <widget> listOfwidgets;
	list <widget> :: iterator p;


	//widgetList.print();

	int choice;
	showMenu();
	cin >> choice;

	while (choice != 6)
	{
		switch (choice)
		{
		case 1:
			getInfo (widgetList, name, type);
			listOfwidgets.push_front(widgetList);
			break;
		case 2:
			listOfwidgets.pop_front();
			break;
		case 3:
			getInfo (widgetList, name, type);
			listOfwidgets.front();
			widgetList.print(listOfwidgets);
			break;
		case 4:
			listOfwidgets.clear();
			break;
		case 5:
			widgetList.print(listOfwidgets);
			break;
		default:
			cout << "Invalid selection."  << endl;
			break;
		}//end switch

		showMenu();
		cin >> choice;
	}//end while


//ERROR HERE, when I try to compile this code I get the following error
//no match for 'operator<<' in 'std::cout << p. std::_List_iterator<_Tp>::operator* [with _Tp = widget]()'	
            for(p = listOfwidgets.begin(); p!=listOfwidgets.end(); ++p)
	        cout << *p << endl;

	//This code added just to make sure things are being added to the stack
	cout << "\nFinal size of listOfwidgets is " << int(listOfwidgets.size()) << endl;


	return 0;
}

void showMenu()
{
	cout << "\n\n****************************************\n"
			<< "\twidget List\n"
			<< "****************************************\n\n";
	cout << "To select an outcome, enter\n";
	cout << "1.  Add widget\n";
	cout << "2.  Remove widget\n";
	cout << "3.  See Top widget\n";
	cout << "4.  Delete List\n";
	cout << "5.  Print List\n";
	cout << "6.  Exit\n";
}//end showMenu

void getInfo (widget& widgetList, string name, double type)
{
	cout << "Enter the widget's name\n";
		cin >> name;
		widgetList.setName(name);


		cout << "\n\nEnter the widget's type\n";
		cin >> type;
		widgetList.setType(type);

}

I also think I am going to have a problem as the iterator is only going to print one output widgetName when I need it to print the name and the type. Since I can't get the current code to work I can't tell if I am correct or not. If I am how would I go about printing both outputs without having to create another list, or would another list need to be created?

cout << *p << endl;

It will help you in getting the value of iterator , and in your case its an object of widget.
So try calling widget methods on this;
eg. (*p).getName()

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.