I'm trying to make use of the STL for linked lists. However, I don't know which of <list> or <list.h> I should be using for linked lists. Should it be <list>? Thanks.
I'm trying to make use of the STL for linked lists. However, I don't know which of <list> or <list.h> I should be using for linked lists. Should it be <list>? Thanks.
It should be <list>.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
When a list is instantiated, is it granted that the list would start out with no node?
if you construct it like this:
then, yes, there are no nodes.Code:list<int> m_list;
Ubuntu Desktop
GCC/G++
Geany (for quick projects)
Anjuta (for larger things)
The list is empty, its size is 0, and there's no data in it. Interestingly enough, that doesn't mean there are no nodes.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thanks for your replies. I'm having a complaint when I'm instantiating my lists as described in my code of a class definition below:
The compiler (Microsoft Visual C++ version 6.0) doesn't seem to link the red lines, even though that the types is a struct type. Do you see anything wrong with the struct types that I've defined?Code:#include <list> struct Item { int PartNumber; int Quantity; int Cost; }; struct LabourItem { int LabourType; int Cost; }; class JobList { private: list<Item> PartOrders; list<LabourItem> Labour; public: JobList(); // Default Constructor void AddItem(int PartNumber, int Quantity, int Cost); void AddLabourItem(int Labour, int Cost); ~JobList(); // Destructor };
Magic might happen if you say "std::list" instead.
Thanks. I probably forgotten about "using namespace std;"
I have another question regarding adding contents in the stl linked lists. For my class definition below:
Let's say that I want to implement the AddItem function:Code:#include <string> #include <vector> #include <exception> #include <list> using namespace std; #ifndef __Job_List_h__ #define __Job_List_h__ #include "Receptionist.h" #include "Technician.h" namespace Main_Class_Diagram { class Receptionist; class Technician; class Job_List; } namespace Main_Class_Diagram { struct Item { int PartNumber; int Quantity; int Cost; }; struct LabourItem { int LabourType; int Cost; }; } namespace Main_Class_Diagram { class Job_List { private: Main_Class_Diagram::Receptionist* _unnamed_Receptionist_; private: Main_Class_Diagram::Technician* _unnamed_Technician_; private: list <Item> PartOrders; list <LabourItem> LabourOrders; public: void AddItem(int PartNumber, int Quantity, int Cost); public: void AddLabourItem(int Labour, int Cost); public: void PrintInvoice(); void NewInvoice(); }; } #endif
For some reason, I can't access the highlights in my IDE to the struct components properly. Does anyone know whether I'm accessing the struct components of PartOrders properly? How would I store those values into the struct contents properly?Code:#include <iostream> #include <string> #include <vector> #include <exception> #include <list> using namespace std; #include "Job_List.h" #include "Receptionist.h" #include "Technician.h" void Main_Class_Diagram::Job_List::AddItem(int PartNumber, int Quantity, int Cost) { PartOrders.push_back(); PartOrders.PartNumber = PartNumber; PartOrders.Cost = Cost; PartOrders.Quantity = Quantity; }
You can't push_back ... nothing. Create an Item, set its fields, and push_back that Item.
Edit: IOW, the list doesn't have any fields, which is why you can't access them. Items have fields.
Last edited by tabstop; 04-10-2008 at 11:37 PM.
That brings me another question: If I want to access any one of the entries, do I have to store the Item into a temporary Item somehow?
No, you should use an iterator to access elements of the list.
By the way, each name that contains a double underscore or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use. As such, instead of using __Job_List_h__ as your header inclusion guard name, use say, JOB_LIST_H_.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)