【问题标题】:Error C7034: an array cannot be initialized with a parenthesized initializer错误 C7034:无法使用带括号的初始化程序初始化数组
【发布时间】:2016-06-23 13:09:23
【问题描述】:

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

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

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(655):错误 C3074:无法使用带括号的初始化程序初始化数组 [C:\xampp\htdocs\enum-windows\ build\enumWindows.vcxproj]

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0(773):注意:参见对函数模板实例化的引用 'void std::allocator<_ty>::construct<_objty>(_Objty (*),char (&)[255])' 正在计算 被解雇了 和 [ _Ty=char [255], _Objty=字符 [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);

也许我的做法很幼稚。

【问题讨论】:

  • 我对 Visual Studio 不熟悉,但我相信它会用行号打印错误,以便您确定是哪一行导致错误。
  • 我推荐std::string的向量。 &amp;s[0] 可以安全地在 C++11 中使用,以使字符串的数据可写。此外,投射到LPSTR 看起来非常可疑。在你的情况下,演员什么都不做。
  • const char* ch = reinterpret_cast&lt;const char*&gt;(windowTitles.at(i)); 看起来不错。我发现您可以通过检查以下编译器错误来查看导致模板实例化错误的行。所以它会像本例一样以 xmemory 开头,然后应该是另一行,可能是另一个标准库,但最终你会在你的一个源文件中获得一个行号。继续阅读。一旦你看到这条线,它就会变得更容易。
  • 看来问题出在windowTitles-&gt;push_back(buff);
  • @flatmouse 仔细查看堆栈跟踪后,您是正确的。

标签: c++ visual-c++ v8


【解决方案1】:

问题出现在这里:

windowTitles->push_back(buff);

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

来自链接的答案:

标准库容器存储的对象必须是可复制的 和可赋值,而数组都不是这些。

也许使用类似下面的东西。数组已替换为std::array&lt;char,255&gt;:

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;
};

【讨论】:

  • 我必须转换为 LPSTR 而不是 LPWSTR。现在它产生了一系列窗口标题,但是有些被截断了。但这是进步!非常感谢。
  • 不客气。我打开了unicode,但我忘记了我更改为LPWSTR进行测试。
猜你喜欢
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
相关资源
最近更新 更多