<分区>
我有一个简单的类,它有一个指向函数的指针。构造函数将其指向函数“morning()”,我在编译时收到错误消息:
error: cannot convert ‘Test::morning’ from type ‘void (Test::)()’ to type ‘Test::function {aka void (*)()}’
Test() {assignPtr = morning;}
当在函数外部声明“morning()”和 typedef 时,代码确实可以编译,我不知道如何让它在当前类中工作。
#include <iostream>
#include <string>
class Test {
public:
Test() {assignPtr = morning;}
void say(std::string a) {name = a; assignPtr();};
private:
typedef void(*function)();
void morning() {std::cout << "Good morning, " << name << std::endl;}
void night() {};
function assignPtr;
std::string name;
};
int main() {
Test a;
a.say("Miguel");
}