Coordinating typedefs and structs in std::multiset (C++)
I'm not a professional programmer, so please don't hesitate to state the obvious.
My goal is t开发者_如何学编程o use a std::multiset
container (typedef EventMultiSet
) called currentEvents
to organize a list of structs, of type Event
, and to have members of class Host
occasionally add new Event
structs to currentEvents
. The structs are supposed to be sorted by one of their members, time. I am not sure how much of what I am trying to do is legal; the g++ compiler reports (in "Host.h") "error: 'EventMultiSet' has not been declared." Here's what I'm doing:
// Event.h
struct Event {
public:
bool operator < ( const Event & rhs ) const {
return ( time < rhs.time );
}
double time;
int eventID;
int hostID;
};
// Host.h
...
void calcLifeHist( double, EventMultiSet * ); // produces compiler error
...
void addEvent( double, int, int, EventMultiSet * ); // produces compiler error
// Host.cpp
#include "Event.h"
...
// main.cpp
#include "Event.h"
...
typedef std::multiset< Event, std::less< Event > > EventMultiSet;
EventMultiSet currentEvents;
EventMultiSet * cePtr = ¤tEvents;
...
Major questions
- Where should I include the EventMultiSet typedef?
- Are my EventMultiSet pointers obviously problematic?
- Is the compare function within my Event struct (in theory) okay?
Thank you very much in advance.
The compiler errors are simply because your typedef is in the wrong place - only main.cpp knows about it. It looks like you probably want it in Event.h, which both of the others include.
I'm not sure exactly what you're asking - but possibly you want to pass by reference not by pointer?
I don't see anything wrong with it - though you might want to provide the other comparisons (
>
,<=
, ...) too.
Given that you solicited statements "of the obvious," one thing I noticed is that you didn't #include <set>
, which is required in order for your compiler to know what a multiset
is, or #include <functional>
which is required to know what less
means:
// main.cpp
#include "Event.h"
#include <set>
#include <functional>
...
typedef std::multiset< Event, std::less< Event > > EventMultiSet;
EventMultiSet currentEvents;
EventMultiSet * cePtr = ¤tEvents;
精彩评论