Working with std::list

Hey there,
I was just wondering if you could give me any insight in how to correctly use std::list from the STL.

I ask this because in my programming studies I was asked to make a LinkedList template class (so I could understand the principals of dynamic memory and doubly-linked lists), since then I've always used that.

I tried using std::list the other day for the first time and I got the error:
include\xmemory0(419): error C2039: 'value_type' : is not a member of 'DocRef'.

I thought there might be a name-clash somewhere but, I wasn't using the std namespace.


I've run out of ideas. I was going to revert back and keep using my template but, I thought I should ask the correct way to use std::list. I assume I'm doing something wrong.

Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

//=========================================================
// Name: DocRef struct
// Desc: A struct used as a reference object to the 
//		 pathway of a virtual document
//=========================================================
struct DocRef
{
	std::string mName;
	std::string mPathway;
};

//=========================================================
// Name: Folder Struct
// Desc: The virtual representation of a folder in a
//       Archive Drawer. Contains a list of sub-folders
//       and Document Pathways.
//=========================================================
struct Folder
{
	std::string               mName;
	std::list<Folder, DocRef> mContents;
};

//=========================================================
// Name: Drawer Struct
// Desc: The virtual representation of a draw in an
//		 Archive. Contains a list of folders, which
//       point to Document Pathways.
//=========================================================
struct Drawer
{
	std::string               mName;
	std::list<Folder, DocRef> mContents;
};

//=========================================================
// Name: Archive Class
// Desc: The Class Declaration of the Archive type,
//       used as a directory for document files.
//=========================================================
class Archive
{
public:
	// Constructors
	//---------------------------------------------
	Archive();
	Archive(const Archive& rhs);

	// Destructors
	//---------------------------------------------
	~Archive();

	// Overloaded Operators
	//---------------------------------------------
	Archive& operator=(const Archive& rhs);

	// Methods
	//---------------------------------------------
	void setName();
	void setPath(char pathway[]);

	void reset();

private:
	std::string                 mName;
	std::string                 mPathway;
	std::list<Drawer>           mContents;
	std::list<Drawer>::iterator mIter;
};


I'm still working on the project (as you can probably tell) so, the header is incomplete.

I'm not sure why but, from reading the errors it appears that std::list has some issue with my "DocRef" struct.

Can you help me?
One thing you should do is.. look at documentation.

For instance: http://en.cppreference.com/w/cpp/container/list

1
2
3
4
5
template<

    class T,
    class Allocator = std::allocator<T>
> class list;


Your DocRef struct is not an allocator.
as cire, said DocRef aint an allocator.
Look at line 22.
Perhaps you meant it to be:
std::list<DocRef> mContents;

?

I.e your folder contains a list of DocRef objects?

My advice (in the short term at least): If you're just learning about std::list, don't worry about allocators.

(but if you do, take a look here:
http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement )
Last edited on
Thanks.
I went through the documentation again after I posted this topic.

I hadn't realised before, that the second argument wasn't another type capture for the container.

I thought that "std::list<int, char> myList" just meant that the list now took char's and int's. Derp.

Thanks for the links. I'll take a look.
I have another question about std::list.
I solved my problem prior issue with taking multiple types in the std::list, by putting them in a struct hierarchy and downcasting.

But when I tried to call pop_front() from my std::list mContents.
I encountered an error:
error C3867: 'std::list<ArchNode,std::allocator<_Ty>>::pop_front': function call missing argument list; use '&std::list<ArchNode,std::allocator<_Ty>>::pop_front' to create a pointer to member


So I followed the instruction of my compiler and rewrote the call to:
 
&std::list<ArchNode>::pop_front;


Why is it requesting this change and is the change going to achieve the same outcome as if I called mContents.pop_front()?
The problem is that you didn't call pop_front

To do so you must include the parentheses as you did in your post, but not in your code:

Topic archived. No new replies allowed.