V8 Hello World编译错误:无法从'v8 :: Local <v8 :: string>'转换为'v8 :: String'</v8 :: string>

时间:2013-12-30 07:34:41

标签: c++ v8

我是V8的新手。我试图运行Hello World示例表单here

但是,我在这一行得到了编译错误:

v8::String source = v8::String::New("'Hello' + ', World'");

error C2440: 'initializing' : cannot convert from 'v8::Local<v8::String>' to 'v8::String'

为什么我收到此错误?

1 个答案:

答案 0 :(得分:3)

String类已更改。它不再具有New方法;它有NewFrom *方法:

static Local< String >  NewFromUtf8 (Isolate *isolate, const char *data, NewStringType type=kNormalString, int length=-1)
static Local< String >  NewFromOneByte (Isolate *isolate, const uint8_t *data, NewStringType type=kNormalString, int length=-1)
static Local< String >  NewFromTwoByte (Isolate *isolate, const uint16_t *data, NewStringType type=kNormalString, int length=-1)

您可以浏览doxygen文档:http://bespin.cz/~ondras/html/classv8_1_1String.html#aa4b8c052f5108ca6350c45922602b9d4

可以通过以下方式创建字符串:

v8::Isolate* i = v8::Isolate::New();//create an isolate instance
//( if you already have one just get it: GetIsolate() 
//=>http://bespin.cz/~ondras/html/classv8_1_1Context.html
v8::Local<v8::String> constant_name = v8::String::NewFromUtf8(i,"asd");
i->Dispose();