我想使用基于Omnet ++的容器cQueue作为优先级队列。 正如API参考和手册中所解释的那样 - 我需要定义它 如下: cQueue queue("Name of queue", someCompareFunc)
someCompareFunc的类型为CompareFunc ,由omnet定义为: typedef int (*CompareFunc)(cObject *a, cObject *b);
所以,我想实现这个比较功能,但没有设法编写可以编译的东西。 我承认我一段时间没有使用函数指针,但经过一个小小的研究,我认为我理解并且确实用eclipse IDE(c ++)编写了一些测试代码。
我正在尝试在simpleModule file.cc上编写代码。 因此,对于我在“常规方式”中在头文件中声明的每个函数,在.cc文件中,我需要在函数名称之前添加带有“::”的模块名称。 所以在我的头文件中我声明: int compareByNodes (cObject *a, cObject *b);
在.cc文件中:
int JobScheduler::compareByNodes (cObject *a, cObject *b){ return 1; };我第一次尝试定义cQueue是:
cQueue queue("job_Buffer",&compareByNodes);但是我收到了一个编译错误:'compareByNodes'未在此范围内声明。 所以我认为它必须与缺少的'JobScheduler ::'有关。 第二次尝试是:
CompareFunc tmp = (CompareFunc)&JobScheduler::compareByNodes; cQueue queue("job_Buffer",tmp);这次我的错误是: “这条线上有多个标记 - 在此背景下 - 从'int(JobScheduler :: )(omnetpp :: cObject ,omnetpp :: cObject *)'转换为'omnetpp :: CompareFunc {aka int( )(omnetpp :: cObject,omnetpp :: cObject *)}'[ - WPMF-转换]”
在“&”之后添加括号也没有帮助。
如果我可以使用Omnet的这种方法,那将真的很有帮助,我想我错过了一些东西,因为它是一个明确定义的软件“功能”。 我也试图谷歌这些主题,在谷歌组部分搜索,并没有找到任何答案。 非常感谢任何帮助
I want to use the Omnet++ based container cQueue as a priority Queue. As it is explained in the API reference and in the manual - I need to define it as follows: cQueue queue("Name of queue", someCompareFunc)
When someCompareFunc is of type CompareFunc which is defined by omnet as: typedef int (*CompareFunc)(cObject *a, cObject *b);
So, I wanted to implement this comparative function, but didn't manage to write something that will compile. I admit I didn't work with function pointers for some time now, but after a small research, I think I do understand and did write some test codes with eclipse IDE (c++).
I'm trying to write the code at a simpleModule file.cc. So for every function I declare at the header file in the "regular way", in the .cc file I need to add the module name with "::" before the function's name. So in my header file I declared: int compareByNodes (cObject *a, cObject *b);
And in the .cc file:
int JobScheduler::compareByNodes (cObject *a, cObject *b){ return 1; };My first try to define the cQueue was:
cQueue queue("job_Buffer",&compareByNodes);But I received a compilation error of: 'compareByNodes' was not declared in this scope. So I figured it must have something to do with the 'JobScheduler::' that is missing. The second try was:
CompareFunc tmp = (CompareFunc)&JobScheduler::compareByNodes; cQueue queue("job_Buffer",tmp);This time my errors were: "Multiple markers at this line - within this context - converting from ‘int (JobScheduler::)(omnetpp::cObject, omnetpp::cObject*)’ to ‘omnetpp::CompareFunc {aka int () (omnetpp::cObject, omnetpp::cObject*)}’ [-Wpmf-conversions]"
Adding parenthesis after the 'ampersand' didn't help either.
It would really help if I could use this method of Omnet and I guess I'm missing something since it's a well defined "feature" of the software. I also tried to google these subjects, searched in the google groups section, and didn't find any answers. Would appreciate any help
最满意答案
尝试将compare方法声明为static。 例如:
//... class Txc1 : public cSimpleModule { protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; public: static int MyCompareFunc (cObject *a, cObject *b); }; Define_Module(Txc1); int Txc1::MyCompareFunc (cObject *a, cObject *b) { return 1; } void Txc1::initialize() { cQueue q1("queue1", MyCompareFunc); // ... }Try to declare a compare method as static. For example:
//... class Txc1 : public cSimpleModule { protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; public: static int MyCompareFunc (cObject *a, cObject *b); }; Define_Module(Txc1); int Txc1::MyCompareFunc (cObject *a, cObject *b) { return 1; } void Txc1::initialize() { cQueue q1("queue1", MyCompareFunc); // ... }更多推荐
发布评论