无法将C ++string向量转换为V8数组

我想在C ++中做一些基本的string处理,并将结果作为一个数组发送到Node.js(V8)。 虽然我可以毫无问题地传递整数和string,但传递C ++string向量并将它们转换为Javascript数组会导致node-gyp投诉太多。

我对C ++的了解很less,我刚刚开始使用V8,那么有人可以填补我缺less的东西吗?

 #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:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.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 Files\Microsoft Visual Studio 11.0\VC\include\xlocale(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 Files\Microsoft Visual Studio 11.0\VC\include\xstring(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:\Users\vijay\.node-gyp\0.10.18\deps\v8\include\v8.h(1225): could be 'v8::Local<T> v8::String::New(const char *,int)' with [ T=v8::String ] D:\Users\vijay\.node-gyp\0.10.18\deps\v8\include\v8.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:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1 gyp ERR! stack at ChildProcess.onExit (D:\Users\vijay\AppData\Roaming\npm\node_modules\node-gyp\lib\build.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())