无法在从 C++ 继承的结构上调用正确的构造函数;班级
我决定尝试使用为面向对象的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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 inV8Context
that forwards thev8::Persistent<T>
argument to its base class.构造函数在 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.new V8Context(...)
只会调用V8Context
中声明的构造函数;构造函数不被继承。您需要添加一个,例如:new V8Context(...)
will only call a constructor declared inV8Context
; constructors are not inherited. You'll need to add one, something like:new V8Context(...)
不会调用v8::Handle
中的构造函数,而是调用V8Context
中的构造函数。由于您尚未定义,因此只有默认构造函数和复制构造函数可用(构造函数不可继承)。因此,它尝试调用复制构造函数并失败,因为参数无法转换为V8Context
。要解决您的问题,您需要向V8Context
添加适当的构造函数,它将参数转发给基本构造函数(可能不是给出的确切代码,具体取决于您的参数的外观,但类似):new V8Context(...)
doesn't call a constructor fromv8::Handle<T>
, but a constructor fromV8Context
. 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 toV8Context
. To solve your problem you need to add an appropriate constructor toV8Context
, which forwards the argument to the base constructor (might not be the exact code given, depending how excatly your arguments look like, but similar):您的
V8Context
仅具有编译器生成的构造函数:V8Cintext
作为参数。您尝试传递的类型似乎与复制构造函数的签名不匹配。如果您想使用它,您需要显式提供相应的构造函数。
Your
V8Context
only has the compiler generated constructors:V8Cintext
as argument.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.