c++ - 错误 C7034 : an array cannot be initialized with a parenthesized initializer

标签 c++ visual-c++ v8

我正在尝试编写一个 native Node 插件,它枚举 Windows 机器上的所有窗口并将它们的标题数组返回给 JS userland。

但是我被这个错误难住了:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(655): error C3074: an array cannot be initialized with a parenthesized initializer [C:\xampp\htdocs\enum-windows\build\enumWindows.vcxproj]

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(773): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,char(&)[255]>(_Objty (*),char (&)[255])' being comp iled with [ _Ty=char [255], _Objty=char [255] ]

据我所知,我没有执行数组的括号初始化?

#include <vector>
#include <node.h>
#include <v8.h>
#include <windows.h>
#include <stdio.h>

using namespace node;
using namespace v8;

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM ptr) {
    std::vector<char[255]>* windowTitles =
        reinterpret_cast<std::vector<char[255]>*>(ptr);

    if (IsWindowVisible(hWnd)) {
        int size = GetWindowTextLength(hWnd);
        char buff[255];
        GetWindowText(hWnd, (LPSTR) buff, size);
        windowTitles->push_back(buff);
    }

    return true;
};

void GetWindowTexts(const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);
    Local<Array> arr = Array::New(isolate);
    std::vector<char[255]> windowTitles;

    EnumWindows(
        &EnumWindowsProc, 
        reinterpret_cast<LPARAM>(&windowTitles));

    for (unsigned int i = 0; i < windowTitles.size(); i++) {
        const char* ch = reinterpret_cast<const char*>(windowTitles.at(i));
        Local<String> str = String::NewFromUtf8(isolate, ch);
        arr->Set(i, str);
    }

    args.GetReturnValue().Set(arr);
}

void init(Handle<Object> exports, Handle<Object> module) {
    NODE_SET_METHOD(module, "exports", GetWindowTexts);
}

NODE_MODULE(enumWindows, init);

我认为错误与这一行有关:

windowTitles->push_back(buff);

也许我的做法很幼稚。

最佳答案

问题出现在这里:

windowTitles->push_back(buff);

因为你cannot store an array in std::vector .

来自链接的答案:

The objects stored by a standard library container must be copyable and assignable, and arrays are neither of these.

也许使用类似下面的东西。该数组已替换为 std::array<char,255> :

BOOL CALLBACK EnumWindowsProc(HWND hWnd,LPARAM ptr)
{
    std::vector<std::array<char,255>>* windowTitles =
        reinterpret_cast<std::vector<std::array<char,255>>*>(ptr);

    if (IsWindowVisible(hWnd)) {
        int size = GetWindowTextLength(hWnd);
        std::array<char,255> buff;
        GetWindowText(hWnd,(LPWSTR)buff.data(),size);
        windowTitles->push_back(buff);
    }

    return true;
};

关于c++ - 错误 C7034 : an array cannot be initialized with a parenthesized initializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37992622/

相关文章:

c++ - 命名空间 'std' 中的“bad_cast”未命名类型错误

c++ - 在 C++ 中 Hook 调用函数?

windows - 是否可以找到 DLL 中声明的结构的大小?

javascript - JavaScript 中对象/数组的性能如何? (专门针对 Google V8)

json - 如何确定 JSON.stringify() 使用哪种编码?

c++ - 帮助!?而控制结构程序!

c++ - PImpl 并不能使我免于导出 STL

没有 "typename"或 "class"的 C++ 模板

c++ - dll 导出/初始化问题(静态变量初始化?)Visual Studio C++

ecmascript-6 - ES6 映射和集合 : how are object keys indexed efficiently?