【问题标题】:Can't convert function pointer argument无法转换函数指针参数
【发布时间】:2009-04-01 18:55:38
【问题描述】:

我得到的错误:

error C2664: 'v8::FunctionTemplate::New' : cannot convert parameter 1 from 'v8::Handle<T> (__cdecl *)(const v8::Arguments &)' to 'v8::InvocationCallback'

相关定义:

typedef Handle<Value> (*InvocationCallback)(const Arguments& args);




template<class C> class V8ScriptClass
{
public:
    template<class C, typename Rtype, typename Ptype1, Rtype (C::*FuncPtr)(Ptype1)> 
    void RegisterFunc(const char* const scriptname)
    {
        objtemplate->Set(
            v8::String::New(scriptname), 
            v8::FunctionTemplate::New(
            V8ScriptClass<C>::RelayCallback<C, Rtype, Ptype1, FuncPtr>
                ));
    };

template<typename Rtype, typename Ptype1, Rtype (*FuncPtr)(Ptype1 param1)>
static v8::Handle<v8::Value> RelayCallback(const v8::Arguments& args)
{
    std::cerr<<__FUNCTION__<<std::endl;
    v8::HandleScope handle_scope;
    return handle_scope.Close(toJSType( ((FuncPtr)(toCType(args[0]))) ));
};

在我看来 typedef 和实际的函数签名是相同的。

编辑:忘记了一个声明:

class EXPORT FunctionTemplate : public Template {
 public:
  /** Creates a function template.*/
  static Local<FunctionTemplate> New(
      InvocationCallback callback = 0,
      Handle<Value> data = Handle<Value>(),
      Handle<Signature> signature = Handle<Signature>());

【问题讨论】:

  • 你在哪里使用 InvocationCallback?
  • 添加了缺失的声明
  • 不仅仅是签名(=0 是默认参数),还有实际的调用。
  • 查看 RegisterFunc 内部,它是 FunctionTemplate::New 构造函数,它接受一个函数指针参数
  • 你到底在哪里得到错误?在实例化点,即您在哪里使用 RegisterFunc?否则,看起来编译器很困惑。您可以尝试使用类型名吗? RelayCallback 在 RegisterFunc 中仍然是不完整的类型。

标签: c++ templates function-pointers v8


【解决方案1】:

我发现了错误。 RelayCallback 模板将静态函数指针作为参数,我尝试使用成员函数指针对其进行实例化。我只需要将其更改为成员函数指针模板参数。

【讨论】:

    猜你喜欢
    • 2019-04-13
    • 2016-09-02
    • 2015-11-03
    • 2021-05-09
    • 2021-04-12
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    相关资源
    最近更新 更多