gpt4 book ai didi

c++ - "a value of type "void (exeCallback::*)(int) "cannot be assigned to an entity of type "void (*)(int) ""

转载 作者:行者123 更新时间:2023-11-30 04:23:11 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Disabling “bad function cast” warning

我正试图将我的大脑围绕在 C++ 函数指针上。为了保持我的学习经验基本,我创建了一个测试函数指针示例。最后,我想通过引用传递所有准备好的实例化对象,这样我就可以回调对象的方法;但是,为了学习和理解,我想坚持使用 C++ 函数指针的基础知识。我创建了一个仅使用 .cpp 文件的工作示例,但我没有成功的部分是在 .cpp 和 .h 中使用函数指针。在使用 .cpp 和 .h 文件时,为了让我的学习示例成功运行,我做错了什么?

我创建了两个文件,exeCallback.h 和 exeCallback.cpp。

.h文件

/*
File: exeCallback.h

Header file for exeCommand Library.
*/

#ifndef EXECALLBACK_H
#define EXECALLBACK_H

#include "mbed.h"

#include <map>

class exeCallback
{
public:
exeCallback();

void my_int_func(int x);

void (*foo)(int);
private:
};

#endif

.cpp 文件:

/*
File: exeCallback.cpp

Execute functions in other Sensor libraries/classes

Constructor
*/

#include "mbed.h"
#include "ConfigFile.h"
#include "msExtensions.h"
#include "cfExtensions.h"
#include "exeCallback.h"

exeCallback::exeCallback()
{

foo = &exeCallback::my_int_func;

/* call my_int_func (note that you do not need to write (*foo)(2) ) */
foo( 2 );

}

void exeCallback::my_int_func(int x)
{
printf( "%d\n", x );
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com