Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ void**不能用作函数_C++_User Interface_Glfw - Fatal编程技术网

C++ void**不能用作函数

C++ void**不能用作函数,c++,user-interface,glfw,C++,User Interface,Glfw,编译此代码时出错。错误显示”&((WidgetButton*)this)->WidgetButton::callback“不能用作函数。我不知道为什么。我做错了什么 老师再解释一下。此代码的目的是能够以面向对象的方式将按钮添加到GUI中回调是赋予构造函数的函数,当按钮被激活(单击)时将调用该函数。虽然现在它只在按下空格键时激活,但为了确保GUI与小部件派生类一起工作 WidgetButton.h #pragma once #include "cgl.h" #include <GLFW/g

编译此代码时出错。错误显示
”&((WidgetButton*)this)->WidgetButton::callback“不能用作函数
。我不知道为什么。我做错了什么

老师再解释一下。此代码的目的是能够以面向对象的方式将按钮添加到GUI中<代码>回调是赋予构造函数的函数,当按钮被激活(单击)时将调用该函数。虽然现在它只在按下空格键时激活,但为了确保GUI与
小部件
派生类一起工作

WidgetButton.h

#pragma once

#include "cgl.h"
#include <GLFW/glfw3.h>

class WidgetButton : Widget
{
    public:
        WidgetButton(GLFWwindow* window, void* callback, int sender);

        void onUpdate(double delta);
        void onRender();

    private:
        void* callback;
        int sender;
};
#pragma一次
#包括“cgl.h”
#包括
类WidgetButton:Widget
{
公众:
WidgetButton(GLFWwindow*窗口、void*回调、int发送方);
无效更新(双三角);
void onRender();
私人:
作废*回拨;
int发送器;
};
WidgetButton.cpp

#pragma once

#include "WidgetButton.h"
#include <GLFW/glfw3.h>

WidgetButton::WidgetButton(GLFWwindow* window, void* callback, int sender) : Widget(window)
{
    this->callback = callback;
    this->sender = sender;
}

void WidgetButton::onUpdate(double delta)
{
    if (glfwGetKey(this->window, GLFW_KEY_SPACE))
        (&this->callback)(sender); //This is where the error happens
}

void WidgetButton::onRender()
{

}
#pragma一次
#包括“WidgetButton.h”
#包括
WidgetButton::WidgetButton(GLFWwindow*窗口,void*回调,int sender):小部件(窗口)
{
这->回调=回调;
此->发送方=发送方;
}
void WidgetButton::onUpdate(双增量)
{
if(glfwGetKey(此->窗口,GLFW_KEY_空格))
(&this->callback)(发送方);//这就是发生错误的地方
}
void WidgetButton::onRender()
{
}

void*
是一种不同于可以容纳函数的类型。尝试将函数指针或类似的东西放入其中是不好的。C++有几种选择,如果你来自C或者没有处理模板太多,最简单的就是这里需要的,函数指针:

void (*callback)(int);
接下来,还可以使用简单的模板执行相同的操作:

template<typename F>
F callback;

指针与函数指针不一样,在C++中,你应该使用比 Vult*/Cuff>更好的东西。code>void*不应用于存储函数指针。那么,如何定义函数指针呢?在我看来,
void**
与函数指针相同。首先,您可以使用反勾号内联代码。第二,可以有一个特定的回调签名吗?如果是这样的话,一个普通的函数指针就足够好了:
void(*callback)(int)
另外,我修复了它。我所做的就是用
void(*callback)(int)
s替换所有
void*
s。我还更改了
(&this->callback)(发送者)
this->callback(this->sender)
。它是1键左边的键`当我这样做并执行
WidgetButton*button=new-WidgetButton(窗口,回调,0)
(回调是一个以1个整数作为参数的无效函数),它表示
没有匹配的函数用于调用'WidgetButton::WidgetButton(GLFWwindow*&int)
。发生了什么?@MonetR.Adams,首先,我建议尽可能不要使用指针和
new
WidgetButton按钮(窗口,回调,0)。其次,看起来您正试图传入一个重载函数。如果必须传入重载函数,则应将其强制转换为正确的类型(基本上与正在调用的函数的类型相同)<代码>静态强制转换(回调)
应该适用于此。函数“callback”位于类内部,并且没有重载。当我用
static\u cast(callback)
强制转换它时,它只会说
无效的static\u cast从类型“”转换为类型“void(*)(int)”
@MonetR.Adams,如果它在一个类中,你需要一个成员函数指针(
void(ClassName::*)(int)
),或者在
std::function
的情况下,您必须考虑
参数。或者您可以将其设置为静态或
std::bind
对象,并将其保存在
std::function
或单个模板参数选项中。我不想将特定类绑定到button类(因为将有多个GUI类使用它)。获取可用于任何类的函数指针的最佳方法是什么?
template<typename Ret, typename... Args>
...
Ret (*callback)(Args...);
std::function<void(int)> callback;