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

vjk2005

我正在尝试在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 [email protected]
gyp info using [email protected] | 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())

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

将C ++ / CLI字符串数组转换为字符串向量

来自分类Dev

在C ++中将字符串向量转换为char数组

来自分类Dev

将字符串转换为int8数组

来自分类Dev

C ++:将字符串转换为向量<double>

来自分类Dev

将字符串向量转换为整数向量

来自分类Dev

Clojure - 将字符串转换为整数向量的向量

来自分类Dev

无法将json数组转换为字符串

来自分类Dev

无法将字符串转换为char数组

来自分类Dev

将数组转换为字符串

来自分类Dev

将字符串转换为数组

来自分类Dev

将数组转换为字符串

来自分类Dev

如何将列向量转换为字符串数组?

来自分类Dev

C ++无法将字符串转换为wstring

来自分类Dev

C# 无法将类型转换为字符串

来自分类Dev

将uint8向量转换为ascii十六进制字符串的更好方法

来自分类Dev

将uint8向量转换为ascii十六进制字符串的更好方法

来自分类Dev

C#JSON导入:无法将字符串转换为字节数组

来自分类Dev

将datetime数组转换为字符串-C#,Win 8

来自分类Dev

C ++将字符串转换为uint8_t数组并返回

来自分类Dev

将字符串转换为数组| C#

来自分类Dev

如何将字符串转换为向量

来自分类Dev

R如何将字符串转换为向量

来自分类Dev

将字符串转换为数值向量

来自分类Dev

将字符串向量转换为整洁格式

来自分类Dev

将包含数字的字符串转换为数字向量

来自分类Dev

将C字符串数组转换为Swift字符串数组

来自分类Dev

将Swift字符串数组转换为C字符串数组指针

来自分类Dev

将Swift字符串数组转换为C字符串数组指针