Can I add a list as a private member of a class

I want to create a c++ class that I need to add an arbitrary number of items of type struct. So I thought I would have a private data member of type list (the standard template library list).

This is how I thought I would code it but I'm getting errors. Could someone explain why I can't do this and possibly describe someway I could accomplish this.

//my code
struct myStruct {
int a;
int b;
};

class myClass {
private:
list<myStruct> myList;
public:
//members not listed
};
Last edited on
Apart from the wrong type of bracket -- ( vs { -- at the start of you class and the missing ; at the end of it, your code looks fine.

What errors are you getting?
Sorry about the bracket and missing semicolon on the above code. I've fixed it.

The error I'm getting in MS visual studios 2010 is:
error C2143: syntax error: missing ';' before '<'
It should compile fine... Maybe it's in the code you've omitted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef MY_HEADER_GUARD_H
#define MY_HEADER_GUARD_H

#include <list>

struct myStruct {
  int a;
  int b;
};

class myClass {
private:
  std::list<myStruct> myList; //namespaces
public:
//members not listed
};

#endif 
Thanks for your help everyone. I got it, or rather ne555 got it.

Adding std:: in front of list is what made the difference. Looks like it was a namespace thing.

thanks again
Topic archived. No new replies allowed.