<分区>
上下文:我正在为 InterruptIn 制作一个开关去抖动包装类,称为 Switch。开关将允许用户附加他们自己的功能,当按下和去抖动真实世界的开关时,这些功能应该被回调。我正在使用 Eclipse C++ 和 arm-none-eabi-* 工具链。
首先,我发现了三种使用 mbed 实现回调的方法:
FunctionPointer 类,已被 ARM 弃用,但 has some documentation (我认为我需要的信息的超链接断开了)。
Callback
<,编译器告诉我这是在尝试使用已弃用的 FunctionPointer 时执行回调的正确方法TM,但没有文档? event_callback_t,没有文档,负责调用用户附加函数的函数,参数是int?
我将使用 Callback
开关.h:
#ifndef MBED_SWITCH_H
#define MBED_SWITCH_H
#include "mbed.h"
class Switch {
private:
Callback<void()> callback_press_;
InterruptIn switch_;
//redacted some debouncing stuff
public:
Switch(PinName pin, bool isActiveHigh);
Switch(PinName pin, bool isActiveHigh, int samplePeriod_us, int sampleCount);
void attachPress(Callback<void()>); //THE PROBLEM FUNCTIONS
template<typename T, typename F>
void attachPress(T* object, F function);
};
#endif
开关.cpp:
#include "Switch.h"
#include "CustomNames.h"
// redacted constructors here
// redacted debouncing functions here
// for attaching a function not associated with any particular class
void Switch::attachPress(Callback<void()> function) {
callback_press_.attach(function);
}
// for attaching a member function of some other class
template<typename T, typename F>
void Switch::attachPress(T* object, F function) {
callback_press_.attach(object, function);
}
问题
如果我使用第一个候选函数 attachPress(Callback
但是,当我尝试将另一个类“Test”的特定成员函数“testfunc1(void)”附加到第二个候选函数时,出现以下错误(我认为来自链接器):
Test.o: In function `Test::Test()':
Test.cpp:(.text._ZN4TestC2Ev+0x1e): undefined reference to `void Switch::attachPress<Test, void (Test::*)()>(Test*, void (Test::*)())'
/home/yankee/programming_projects/cpp-eclipse/SO_EXAMPLE/Makefile:97: recipe for target 'HW4_RACE.elf' failed
collect2: error: ld returned 1 exit status
Makefile:21: recipe for target 'all' failed
make[1]: *** [HW4_RACE.elf] Error 1
make: *** [all] Error 2
03:10:35 Build Finished (took 165ms)
为什么这会给我“ undefined reference ”,有没有办法解决它?