协调 std::multiset 中的 typedef 和结构 (C++)

发布于 2024-08-28 18:21:53 字数 1106 浏览 15 评论 0原文

我不是专业程序员,所以请毫不犹豫地陈述显而易见的事情。

我的目标是使用名为 currentEventsstd::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 = &currentEvents;
...

主要问题

  1. 我应该在哪里包含 EventMultiSet typedef?
  2. 我的 EventMultiSet 指针明显有问题吗?
  3. 我的事件结构中的比较函数(理论上)可以吗?

预先非常感谢您。

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

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网白 2024-09-04 18:21:53
  1. 编译器错误只是因为你的 typedef 位于错误的位置 - 只有 main.cpp 知道它。看起来您可能希望将其放在 Event.h 中,其他两个都包含该文件。

  2. 我不确定您到底在问什么 - 但可能您想要通过引用传递而不是通过指针?

  3. 我认为它没有任何问题 - 尽管您可能也想提供其他比较(><=,...)。

  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.

太阳公公是暖光 2024-09-04 18:21:53

鉴于您征求了“显而易见的”陈述,我注意到的一件事是您没有 #include,这是您的编译器需要知道什么是 multiset 是,或 #include ,需要了解 less 的含义:

// main.cpp
#include "Event.h"
#include <set>
#include <functional>
...
typedef std::multiset< Event, std::less< Event > > EventMultiSet;
EventMultiSet currentEvents;
EventMultiSet * cePtr = ¤tEvents;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文