C++ C++;指向函数作为参数的指针。数据类型不兼容

C++ C++;指向函数作为参数的指针。数据类型不兼容,c++,function,pointers,arguments,C++,Function,Pointers,Arguments,我在查找代码中的错误原因时遇到了一些困难,我想向您寻求帮助。昨天我已经问过类似的问题,但后来我想我找到了原因。今天早上编译器再次证明我错了 错误是这样的: IntelliSense: argument of type "void (Transaction::*)()" is incompatible with parameter of type "eventPointer" error C2664: 'Calendar::calendarPush' : cannot convert param

我在查找代码中的错误原因时遇到了一些困难,我想向您寻求帮助。昨天我已经问过类似的问题,但后来我想我找到了原因。今天早上编译器再次证明我错了

错误是这样的:

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);

可以在中找到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;
}
我只是不明白这样的事情是如何发生的,它似乎与我的代码所包含的内容直接不一致

我宁愿重新粘贴整个代码,以防我搞砸了我看不见的东西:

----------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() {

}   
------------MAIN.CPP----------
#包括
#包括“Calendar.h”
#包括“Transaction.h”
使用名称空间std;
int main(){

cout问题在于它们是指向成员函数的指针。尽管它们具有相同的返回类型和参数类型,但它们被视为不同的类型,因为它们是不相关类的成员

当您指定参数的类型为
void(Calendar::*)()
时,只有Calendar的成员函数可以转换为它

解决此问题的一种方法是,
Calendar
采用
void(*)(
而不是
void(Calendar:*)()
。然后将
事务的
事件成员函数更改为静态函数。然后
事件
函数的类型将为
void(*)()
而不是
作废(交易::*)()


另一种方法是将
eventPointer
更改为
void(Transaction::*)()
类型,以便转换工作。此外,您的
Transaction.cpp
当前似乎定义了
void event1();
而不是
void Transaction::event1()
这将导致一些未定义的函数相关错误。

“我看不出这两个函数是如何不兼容的。”它们是。A类的指针指向成员函数与B类的指针指向成员函数不兼容(A和B之间存在关系时除外)。问题是,当我将Calendar.h中的eventPointer声明更改为typedef void(事务::*eventPointer)();这个问题仍然存在。由于我必须在日历中包含Transaction.h。h我得到了更多的错误,比如错误C2027:使用未定义的类型“Transaction”,错误C2011:“Transaction”:“class”类型重新定义,后跟类型为“void(Transaction::*)()”的旧参数与类型为“eventPointer”的参数不兼容错误C2664:“Calendar::calendarPush”:无法将参数3从“void(uu cdecl*)(void)”转换为“eventPointer”。太混乱了。请给出一个代码示例,说明“void(*)()”@Jammi:如果我们有一个函数:
void f(){}
,那么
f
的类型是
void()
&f
的类型(
f
的地址)是
void(*)(
)。
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() {

}