无法C++字符串向量转换为 V8 数组

Cannot convert C++ string vector to V8 array

本文关键字:V8 数组 转换 向量 C++ 字符串 无法      更新时间:2023-10-16

我正在尝试在C++中进行一些基本的字符串处理,并将结果作为数组发送到 Node.js (V8)。虽然我可以毫无问题地传递整数和字符串,但传递C++字符串向量并将它们转换为 Javascript 数组会导致node-gyp抱怨很多。

我对C++的了解非常不完整,我刚刚开始使用 V8,所以有人可以填写我缺少的内容吗?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <node.h>
#include <v8.h>
using namespace std;
using namespace v8;
Handle<Value> uniques(const Arguments& args) {
    HandleScope scope;
    vector<string> words;
    // open text file with lots of words separated by spaces
    ifstream openFile( "D:\words" );
    if( openFile.is_open() ) {
        while( !openFile.eof() ) {
            string temp;
            openFile >> temp;
            if( temp != "" ) {
                words.push_back( temp );
            }
        }
    }
    // Filter out duplicates
    sort( words.begin(), words.end() );
    words.erase( unique( words.begin(), words.end() ), words.end() );
    // Convert string Vector to V8 array
    Handle<Array> wordArray = Array::New( words.size() );
    for (unsigned i=0; i < words.size(); i++) {
        // The problem area: this next line is where node-gyp starts moaning.
        wordArray->Set( Number::New(i), String::New(words[i]) );
    }
    return scope.Close( wordArray );
}

// setup Node.js module
void init(Handle<Object> exports) {
    exports->Set(String::NewSymbol("words"), FunctionTemplate::New(uniques)->GetFunction());
}

输出:

gyp info using node-gyp@0.10.10
gyp info using node@0.10.18 | win32 | ia32
gyp info spawn D:WindowsMicrosoft.NETFrameworkv4.0.30319msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=Win32' ]
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
  words.cc
D:Program FilesMicrosoft Visual Studio 11.0VCincludexlocale(336): warning C4530: C++ exception handler used, but unwind semantics are
not enabled. Specify /EHsc [D:words.vcxproj]
..words.cc(14): error C3203: 'basic_string' : unspecialized class template can't be used as a template argument for template parameter '_T
y', expected a real type [D:words.vcxproj]
..words.cc(14): error C2955: 'std::basic_string' : use of class template requires template argument list [D:words.vcxproj]
          D:Program FilesMicrosoft Visual Studio 11.0VCincludexstring(698) : see declaration of 'std::basic_string'
..words.cc(22): error C2664: 'void std::vector<_Ty>::push_back(int &&)' : cannot convert parameter 1 from 'std::string' to 'int &&' [D:words.vcxproj]
          with
          [
              _Ty=int
          ]
          Reason: cannot convert from 'std::string' to 'int'
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
..words.cc(34): error C2665: 'v8::String::New' : none of the 2 overloads could convert all the argument types [D:words.vcxproj]
          D:Usersvijay.node-gyp.10.18depsv8includev8.h(1225): could be 'v8::Local<T> v8::String::New(const char *,int)'
          with
          [
              T=v8::String
          ]
          D:Usersvijay.node-gyp.10.18depsv8includev8.h(1228): or       'v8::Local<T> v8::String::New(const uint16_t *,int)'
          with
          [
              T=v8::String
          ]
          while trying to match the argument list '(int)'
gyp ERR! build error
gyp ERR! stack Error: `D:WindowsMicrosoft.NETFrameworkv4.0.30319msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (D:UsersvijayAppDataRoamingnpmnode_modulesnode-gyplibbuild.js:267:23)
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:98:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:789:12)
gyp ERR! System Windows_NT 6.1.7600

>v8::String::New有不同的签名。看这里。

像这样尝试:

String::New(&words[i][0], words[i].size())