我在找到代码中的错误原因时遇到了一些麻烦,我想请求您的帮助。昨天我已经问过类似的问题,但后来我想我找到了原因。编译器今天早上再次证明我错了......
错误说:
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'
导致错误的代码可以在
中找到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() {
}
Ty提供任何帮助。
答案 0 :(得分:3)
问题是因为它们是指向成员函数的指针。虽然它们具有相同的返回类型和参数类型,但它们被认为是不同的类型,因为它们是不相关类的成员。
当您指定参数的类型为void (Calendar::*)()
时,只有Calendar的成员函数可以转换为它。
解决问题的一种方法是Calendar
取void (*)()
而不是void (Calendar::*)()
。然后将event
的{{1}}成员函数更改为静态函数。 Transaction
函数的类型将为event
,而不是void (*)()
。
另一种方法是将void (Transaction::*)()
更改为eventPointer
类型,以便转换生效。此外,您的void (Transaction::*)()
目前似乎定义Transaction.cpp
而不是void event1();
,这将导致一些未定义的函数相关错误。