変数宣言が間違っています。変数は通常、次の構文で宣言されます。
std::vector<int> IDM_POPUP_LAST (5);
それに加えて、for
単に関数の外に置くことはできません。
そうは言っても、これはおそらくグローバル変数として使用するものです。これを回避する1つの方法は、初期化する関数を使用して、クラスの静的メンバーにすることです。必要に応じて、他のタイプのIDをここに追加し、必要に応じて関数に初期化を追加することもできます。
//resource.h
struct ResourceIDs {
static std::vector<int> menus;
static void init();
//Let's add in three cursors
static std::vector<int> cursors;
};
//NOTE - these are all better off in a .cpp
#include "resources.h" //if in the cpp
std::vector<int> ResourceIDs::menus (5); //define the menus member
std::vector<int> ResourceIDs::cursors (3); //define the cursors member
void ResourceIDs::init() {
for (int i = 0; i < menus.size(); ++i) //note the type mismatch since vector
menus[i] = i + 90; //uses size_t, which is unsigned
//let's do cursors starting at 150
for (int i = 0; i < cursors.size(); ++i)
cursors[i] = i + 150;
}
今、あなたはそれらを初期化することを確認する必要があります、そしてそれからあなたはあなたが必要とするところならどこでもそれらを使うことができます:
#include <windows.h>
#include "resource.h"
int main() {
ResourceIDs::init();
//create window, message loop, yada yada
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_COMMAND:
//check the menu ids using ResourceIDs::menus[x]
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
}
ここでのIDの#define-ingのコードとの唯一の違いはResourceIDs::init()
、の先頭での呼び出しmain
、の必要性ResourceIDs::
、および配列構文です。