协调 std::multiset 中的 typedef 和结构 (C++)
我不是专业程序员,所以请毫不犹豫地陈述显而易见的事情。
我的目标是使用名为 currentEvents
的 std::multiset
容器 (typedef EventMultiSet
) 来组织 类型的结构列表Event
,并且让 Host
类的成员偶尔向 currentEvents
添加新的 Event
结构。这些结构应该按其成员之一(时间)排序。我不确定我想做的事情有多少是合法的; g++ 编译器报告(在“Host.h”中)“错误:‘EventMultiSet’尚未声明。”这就是我正在做的事情:
// 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;
...
主要问题
- 我应该在哪里包含 EventMultiSet typedef?
- 我的 EventMultiSet 指针明显有问题吗?
- 我的事件结构中的比较函数(理论上)可以吗?
预先非常感谢您。
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 = ¤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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器错误只是因为你的 typedef 位于错误的位置 - 只有 main.cpp 知道它。看起来您可能希望将其放在 Event.h 中,其他两个都包含该文件。
我不确定您到底在问什么 - 但可能您想要通过引用传递而不是通过指针?
我认为它没有任何问题 - 尽管您可能也想提供其他比较(
>
、<=
,...)。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.鉴于您征求了“显而易见的”陈述,我注意到的一件事是您没有
#include
,这是您的编译器需要知道什么是multiset
是,或#include
,需要了解less
的含义: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 amultiset
is, or#include <functional>
which is required to know whatless
means: