Define a global 'set' variable

Hi All,

I am trying to define a global 'set' object to use it throughout my code. Here is the example:

In my main.h include file:
struct StudentStruct {
string name;
string lname;
};

set <StudentStruct> studentSet;

-------------------------------------
In my all CPP files:
#include "main.h"

student.insert(st1);
student.insert(st2);

But as soon as I do it it will gives me error saying 'expected definition before '<' in main.h.
How can I define a global 'set' variable?


Thanks in advance.
I think you need to add a compare function in your struct in order to use it with sets. Check out the sets reference: http://www.cplusplus.com/reference/stl/set/
That error means you probably forgot to #include <set> or that you need to put std:: at the front of set.

With that declaration of studentSet, it won't be global. You need to give it external linkage, i.e. in a .h file put

extern std::set <StudentStruct> studentSet;

Then, you need to define it in a .cpp file like this

std::set <StudentStruct> studentSet;
Last edited on
Topic archived. No new replies allowed.