首页 > 解决方案 > “void (Test::*)()”类型的值不能分配给“void (*)()”类型的实体

问题描述

函数指针有问题,我想在函数上创建一个指针,但是当我试图指向必须指向哪个函数指针时,下一个文本出现错误:“void (Test:: )()类型的值" 不能分配给类型为 "void ( )()" 的实体。这是我的代码:

testh.h:
#pragma once
#include<iostream>
using namespace std;
class Test {    
public:
    Test();
    void hello();
    void goodBye();
    ~Test();
private:
    void(*b1)();
    void(*b2)();
    void(*functPtrs[1])();
    
};

test.cpp:

#include"testh.h"
Test::Test() {
    b1 = hello;
    b2 = goodBye;
    functPtrs[0] = b1;
    functPtrs[1]=b2;

}
void Test::hello() {
    cout << "hello" << endl;

}
void   Test::goodBye() {
    cout << "Goodbye" << endl;
}
Test::~Test() {

}

Thanks in advance

标签: c++pointers

解决方案


推荐阅读