【发布时间】:2013-12-01 11:50:27
【问题描述】:
我在查找代码中的错误原因时遇到了一些麻烦,我想向您寻求帮助。我昨天已经问过类似的问题,但后来我想我找到了原因。今天早上编译器再次证明我错了......
错误说:
IntelliSense: argument of type "void (Transaction::*)()" is incompatible with parameter of type "eventPointer"
error C2664: 'Calendar::calendarPush' : cannot convert parameter 3 from 'void (__thiscall Transaction::* )(void)' to 'eventPointer'
导致错误的代码可以在 eb 中找到
Main.cpp, line (calling of the method)
Calendar::calendarPush(1, 1, &Transaction::event1);
和
Calendar.cpp line (definition of the method)
void Calendar::calendarPush(double Time, int Priority, eventPointer event)
eventPointer的定义见
Calendar.h, line
typedef void (Calendar::*eventPointer) ();
我看不出这两者是如何不相容的。怎么了? :/
代码如下:
----------MAIN.CPP----------
#include <iostream>
#include "Calendar.cpp"
#include "Transaction.cpp"
using namespace std;
int main() {
cout << "Initializing" << endl;
double Time = 0.0;
int Priority = 0;
cout << "Pushing initial event" << endl;
Calendar::calendarPush(1, 1, &Transaction::event1);
}
----------CALENDAR.H----------
#include <iostream>
#include <queue>
using namespace std;
class Calendar;
typedef void (Calendar::*eventPointer) ();
struct activationRecord {
double Time;
int Priority;
eventPointer activationEvent;
};
bool operator < (const activationRecord & a, const activationRecord & b);
class Calendar {
private:
static std::priority_queue<activationRecord> activationCalendar;
public:
bool calendarEmpty();
static void calendarPush(double, int, eventPointer);
activationRecord calendarTop();
void calendarPop();
void calendarRun();
};
----------CALENDAR.CPP-----------
#include "Calendar.h"
bool Calendar::calendarEmpty() {
return activationCalendar.empty();
}
void Calendar::calendarPush(double Time, int Priority, eventPointer event) {
activationRecord record;
record.Time = Time;
record.Priority = Priority;
record.activationEvent = event;
activationCalendar.push(record);
}
activationRecord Calendar::calendarTop() {
return activationCalendar.top();
}
void Calendar::calendarPop() {
activationCalendar.pop();
}
void Calendar::calendarRun() {
activationRecord record;
while(!calendarEmpty()) {
record = calendarTop();
calendarPop();
cout << record.Time << endl;
cout << record.Priority << endl;
cout << record.activationEvent << endl << endl;
}
}
bool operator < (const activationRecord & a, const activationRecord & b) {
return a.Time > b.Time;
}
----------TRANSACTION.H----------
#include <iostream>
using namespace std;
class Transaction {
public:
void event1();
void event2();
void event3();
void event4();
void event5();
};
----------TRANSACTION.CPP-----------
#include <iostream>
#include "Transaction.h"
using namespace std;
void event1() {
cout << "event1" << endl;
}
void event2() {
cout << "event2" << endl;
}
void event3() {
cout << "event3" << endl;
}
void event4() {
cout << "event4" << endl;
}
void event5() {
cout << "event5" << endl;
}
现在突然间我遇到了更多的错误,这对我来说更没有意义。
error C2027: use of undefined type 'Transaction'
error C2065: 'event1' : undeclared identifier
error C2011: 'Transaction' : 'class' type redefinition
我只是不明白怎么会发生这样的事情,这似乎与我的代码包含的内容直接不一致。
我宁愿再次粘贴整个代码,以防我搞砸了我看不到的东西:
----------MAIN.CPP----------
#include <iostream>
#include "Calendar.h"
#include "Transaction.h"
using namespace std;
int main() {
cout << "Inicializuji" << endl;
double Time = 0.0;
int Priority = 0;
cout << "Vlkadam uvodni udalost" << endl;
Calendar::calendarPush(1, 1, &Transaction::event1);
}
----------CALENDAR.H----------
#include <iostream>
#include <queue>
#include "Transaction.h"
using namespace std;
typedef void (Transaction::*eventPointer) ();
struct activationRecord {
double Time;
int Priority;
eventPointer activationEvent;
};
bool operator < (const activationRecord & a, const activationRecord & b);
class Calendar {
private:
static std::priority_queue<activationRecord> activationCalendar;
public:
bool calendarEmpty();
static void calendarPush(double, int, eventPointer);
activationRecord calendarTop();
void calendarPop();
void calendarRun();
};
----------CALENDAR.CPP-----------
#include "Calendar.h"
bool Calendar::calendarEmpty() {
return activationCalendar.empty();
}
void Calendar::calendarPush(double Time, int Priority, eventPointer event) {
activationRecord record;
record.Time = Time;
record.Priority = Priority;
record.activationEvent = event;
activationCalendar.push(record);
}
activationRecord Calendar::calendarTop() {
return activationCalendar.top();
}
void Calendar::calendarPop() {
activationCalendar.pop();
}
void Calendar::calendarRun() {
activationRecord record;
while(!calendarEmpty()) {
record = calendarTop();
calendarPop();
cout << record.Time << endl;
cout << record.Priority << endl;
cout << record.activationEvent << endl << endl;
}
}
bool operator < (const activationRecord & a, const activationRecord & b) {
return a.Time > b.Time;
}
----------TRANSACTION.H----------
#include <iostream>
using namespace std;
class Transaction {
public:
void event1();
};
----------TRANSACTION.CPP-----------
#include "Transaction.h"
using namespace std;
void Transaction::event1() {
}
请帮忙。
【问题讨论】:
-
“我看不出这两个怎么不兼容。”他们是。 Pointer-to-member-function-of-class-A 与 Pointer-to-member-function-of-class-B 不兼容(除非 A 和 B 之间存在关系)。
-
问题是,当我将 Calendar.h 中 eventPointer 的声明更改为 typedef void (Transaction::*eventPointer) ();。问题仍然存在。因为我必须在 Calendar.h 中包含 Transaction.h,所以我遇到了更多错误,例如错误 C2027:使用未定义类型“事务”,错误 C2011:“事务”:“类”类型重新定义,然后是旧的“void (Transaction::*)()”类型的参数与“eventPointer”类型的参数不兼容,错误 C2664:“Calendar::calendarPush”:无法将参数 3 从“void (__cdecl *)(void)”转换为'eventPointer'. 所以很困惑。
标签: c++ function pointers arguments