无法在从 C++ 继承的结构上调用正确的构造函数;班级

发布于 2024-12-26 08:00:55 字数 1186 浏览 4 评论 0原文

我决定尝试使用为面向对象的 C++ 代码开发 C 包装 API。不幸的是,我对 C++ 不太熟悉,所以我遇到了继承构造函数的问题。

v8capi.h

typedef struct V8Context V8Context;

#ifdef __cplusplus
extern "C" {
#endif

V8Context *V8_NewContext();

#ifdef __cplusplus
}
#endif

v8capi.cpp

#include <v8.h>

struct V8Context : public v8::Handle<v8::Context> { };

V8Context *V8_NewContext() {
    v8::HandleScope hscope;
    return new V8Context(v8::Context::New());
}

据我了解, new V8Context(...) 应该调用 v8::Handle 的构造函数,该构造函数采用 句柄v8::Context::New() 返回一个 v8::Persistent,它继承 v8::Handle,这样应该可以。但实际上,它试图调用一个采用 const V8Context & 的构造函数:

error C2664: 'V8Context::V8Context' : cannot convert parameter 1 from
'v8::Persistent<T>' to 'const V8Context &'
    with
    [
        T=v8::Context
    ]
    No user-defined-conversion operator available that can perform this
    conversion, or the operator cannot be called

我做错了什么?

I decided to try creating a simple C wrapper for the V8 API using the model described in Developing C wrapper API for Object-Oriented C++ code. Unfortunately, I'm not too familiar with C++, so I'm running into an issue with inherited constructors.

v8capi.h

typedef struct V8Context V8Context;

#ifdef __cplusplus
extern "C" {
#endif

V8Context *V8_NewContext();

#ifdef __cplusplus
}
#endif

v8capi.cpp

#include <v8.h>

struct V8Context : public v8::Handle<v8::Context> { };

V8Context *V8_NewContext() {
    v8::HandleScope hscope;
    return new V8Context(v8::Context::New());
}

From what I understand, new V8Context(...) should call v8::Handle<T>'s constructor which takes a Handle<T>. v8::Context::New() returns a v8::Persistent<T>, which inherits v8::Handle<T>, so that should work. But in reality, it's trying to call a constructor that takes a const V8Context &:

error C2664: 'V8Context::V8Context' : cannot convert parameter 1 from
'v8::Persistent<T>' to 'const V8Context &'
    with
    [
        T=v8::Context
    ]
    No user-defined-conversion operator available that can perform this
    conversion, or the operator cannot be called

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼中杀气 2025-01-02 08:00:55

V8Context 中没有定义构造函数,因此只有隐式复制构造函数作为选项出现。您需要在 V8Context 中显式定义一个构造函数,将 v8::Persistent 参数转发到其基类。

There is no constructor defined in V8Context, so only the implicit copy constructor appears as an option. You need to explicitly define a constructor in V8Context that forwards the v8::Persistent<T> argument to its base class.

故事还在继续 2025-01-02 08:00:55

构造函数在 C++ 中不是继承的(尽管我相信 C++11 中有一些直通功能),因此您必须在 V8Context 中编写自己的构造函数来显式调用正确的基类类构造函数。

Constructors aren't inherited in C++ (although there are some passthrough features in C++11 I believe), so you're going to have to write your own constructor in V8Context that explicitly calls the proper base class constructor.

长发绾君心 2025-01-02 08:00:55

据我了解,new V8Context(...) 应该调用 v8::Handle 的构造函数。

new V8Context(...) 只会调用 V8Context 中声明的构造函数;构造函数不被继承。您需要添加一个,例如:

V8Context(v8::Handle<v8::Context> const & handle) : 
    v8::Handle<v8::Context>(handle) 
{}

From what I understand, new V8Context(...) should call v8::Handle<T>'s constructor.

new V8Context(...) will only call a constructor declared in V8Context; constructors are not inherited. You'll need to add one, something like:

V8Context(v8::Handle<v8::Context> const & handle) : 
    v8::Handle<v8::Context>(handle) 
{}
鲜血染红嫁衣 2025-01-02 08:00:55

new V8Context(...) 不会调用 v8::Handle 中的构造函数,而是调用 V8Context 中的构造函数。由于您尚未定义,因此只有默认构造函数和复制构造函数可用(构造函数不可继承)。因此,它尝试调用复制构造函数并失败,因为参数无法转换为 V8Context。要解决您的问题,您需要向 V8Context 添加适当的构造函数,它将参数转发给基本构造函数(可能不是给出的确切代码,具体取决于您的参数的外观,但类似):

struct V8Context : public v8::Handle<v8::Context> {
  V8Context(const v8::Handle<v8::Context>& handle): v8::Handle<v8::Context>(handle) {}
};

new V8Context(...) doesn't call a constructor from v8::Handle<T>, but a constructor from V8Context. Since you haven't defined one, only the default constructor and copy constructor are availible (constructors aren't inheritable). Therefore it tries to call the copy constructor and fails since the argument can't be converted to V8Context. To solve your problem you need to add an appropriate constructor to V8Context, which forwards the argument to the base constructor (might not be the exact code given, depending how excatly your arguments look like, but similar):

struct V8Context : public v8::Handle<v8::Context> {
  V8Context(const v8::Handle<v8::Context>& handle): v8::Handle<v8::Context>(handle) {}
};
烟柳画桥 2025-01-02 08:00:55

您的 V8Context 仅具有编译器生成的构造函数:

  • 复制构造函数在需要时合成,并且未显式声明或使其不可能(例如,通过从没有复制构造函数的类派生)。此构造函数采用 V8Cintext 作为参数。
  • 默认构造函数,在需要时合成,当没有显式构造函数时,所有成员和基类都可以默认构造。该构造函数没有参数。

您尝试传递的类型似乎与复制构造函数的签名不匹配。如果您想使用它,您需要显式提供相应的构造函数。

Your V8Context only has the compiler generated constructors:

  • the copy constructor which is synthesized whenever it is needed and not explicitly declared or made impossible (e.g. by deriving from a class without a copy constructor). This constructor take a V8Cintext as argument.
  • the default constructor which is synthesized when needed, when there is no explicit constructor, and all members and bases can be default constructed. Thos constructor has no arguments.

It seems the type you try to pass doesn't match the signature of the copy constructor. If you want to use it you'll need to explicitly provide a corresponding constructior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文