tôi muốn làm một lớp lưu trữ con trỏ hàm, nhưng khi tôi muốn lưu trữ chúng trong các biến thành viên tôi nhận được lỗi này:C++ Gán con trỏ hàm để
invalid use of member function (did you forget the '()' ?)
¿đâu là lỗi của tôi?
class Button {
public:
Button(PS3USB * ps3, ButtonEnum button, void (*onPress)(void) = nullptr,
void (*onRelease)(void) = nullptr) {
PS3 = ps3;
status = false;
ERROR ---> onPressFunction = onPress; <--- ERROR
ERROR ---> onReleaseFunction = onRelease; <--- ERROR
id = button;
}
void check() {
if (PS3->getButtonClick(id) && !status) {
if (onPressFunction != nullptr) {
onPressFunction();
}
status = !status;
} else if (!PS3->getButtonClick(id) && status) {
if (onReleaseFunction != nullptr) {
onReleaseFunction();
}
status = !status;
}
}
private:
bool status;
PS3USB * PS3;
ButtonEnum id;
void * onPressFunction(void);
void * onReleaseFunction(void);
};
Cảm ơn
Bạn có thể thêm vào những gì dòng lỗi xảy ra? Ngoài ra nếu bạn sử dụng C++ 11, bạn nên có một cái nhìn tại 'std :: function' thay vì con trỏ funtion thô. – Hayt
Bạn thực sự nên sử dụng một 'typedef' cho các con trỏ hàm của bạn. Điều đó cũng đã giải quyết vấn đề này. Hoặc thậm chí sử dụng 'std :: function' nếu bạn có thể. – nvoigt