基本示例的V8编译错误

时间:2012-07-02 03:44:51

标签: c++ v8

我正在尝试编译V8的hello world示例,并且我一直遇到编译时错误。这是代码:

#include <v8/src/v8.h>

using namespace v8;

int main(int argc, char* argv[]) {

  // Create a string holding the JavaScript source code.
  String source = String::New("Hi");

  // Compile it.
  Script script = Script::Compile(source) ;

  // Run it.
  Value result = script->Run();

  // Convert the result to an ASCII string and display it.
  String::AsciiValue ascii(result) ;
  printf("%s\n", *ascii) ;
  return 0;
}

这是编译错误:

error: conversion from ‘v8::Local<v8::String>’ to non-scalar type ‘v8::String’ requested

错误在第8行,其中包含:String source = String :: New(“Hi”);

我试过google'ing这个错误毫无意义,似乎无法找到一个有意义的修复。有什么想法吗?

我试过了两个:

svn checkout http://v8.googlecode.com/svn/trunk/ v8

svn checkout http://v8.googlecode.com/svn/branches/bleeding_edge/ v8

并为两者获得相同的错误。

2 个答案:

答案 0 :(得分:1)

根据错误消息,尝试:

Local<String> source = String::New("Hi");

答案 1 :(得分:0)

试试这段代码:

HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
Handle<String> source = String::New("'Hello' + ', World!'");
Handle<Script> script = Script::Compile(source);
TryCatch trycatch;
Handle<Value> result = script->Run();   
if ( result.IsEmpty() ) {
    Handle<Value> excep = trycatch.Exception();
    String::AsciiValue excep_str(excep);
    printf("%s\n",*excep);
}  else {
    String::AsciiValue ascii(result);
    printf("%s\n", *ascii);
}
context.Dispose();
return 0;