Question

I'm not a professional programmer, so please don't hesitate to state the obvious.

My goal is to 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 = &currentEvents;
...

Major questions

  1. Where should I include the EventMultiSet typedef?
  2. Are my EventMultiSet pointers obviously problematic?
  3. Is the compare function within my Event struct (in theory) okay?

Thank you very much in advance.

Was it helpful?

Solution

  1. 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.

  2. I'm not sure exactly what you're asking - but possibly you want to pass by reference not by pointer?

  3. I don't see anything wrong with it - though you might want to provide the other comparisons (>, <=, ...) too.

OTHER TIPS

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 = &currentEvents;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top