2016-06-23 67 views
4

Windowsマシン上のすべてのウィンドウを列挙するネイティブのノードアドオンを作成しようとしていて、JSのユーザーランドにタイトルの配列を返します。エラーC7034:カッコで囲まれた初期化子で配列を初期化できません

は、しかし、私はこのエラーで困惑しています:私の知る限り

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

はおそらく、私のアプローチは、ナイーブです。

+0

私は、Visual Studioに慣れていないんだけど、私はあなたが*どの行でエラーが発生*確認することができますように、それは、行番号とエラーを印刷するものと信じている:配列はstd::array<char,255>に置き換えられました。 – Mine

+1

'std :: string'のベクトルをお勧めします。文字列のデータを書き込み可能にするには、 '&s [0]'をC++ 11で安全に使うことができます。また、 'LPSTR'へのキャストは非常に疑わしく見えます。あなたの場合、キャストは何もしていません。 – chris

+0

'const char * ch = reinterpret_cast (windowTitles.at(i));' OKです。私は、次のコンパイラエラーをチェックすることによってどのインスタンスがテンプレートのインスタンス化エラーを引き起こしているのかを知ることができます。だから、この場合のようにxmemoryで始まり、別の標準ライブラリのために別の行が続くはずですが、最終的にソースファイルの1つに行番号が入ります。ただ読書を続けてください。一度線が見えたら、それはより簡単になります。 – wally

答えて

4

ここでの問題は生じ:

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.

おそらく次のようなものを使用します。リンクの答えから

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

私は 'LPWSTR'ではなく' LPSTR'にキャストしなければなりませんでした。そして今、それはウィンドウタイトルの配列を生成しますが、いくつかは切り詰められます。しかし、それは進歩です!親切にありがとう。 – sdgluck

+1

大歓迎です。私はユニコードをオンにしましたが、私はテストするために 'LPWSTR'に変更したことを忘れました。 – wally

関連する問題