0

How do I declare a contiguous range of Resource ID's in an array in a header file? I'm appending these RID's to a menu.

crr
  • 9
  • 4
  • 1
    Can you clarify what is a "Resource ID" and perhaps show us what you have done so far and where you are stuck? – Mysticial Jul 22 '12 at 02:59
  • 1
    Honestly, it just sounds like you want an [array](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c). – chris Jul 22 '12 at 03:00
  • #define IDM_POPUP_LAST1 74 #define IDM_POPUP_LAST2 75 #define IDM_POPUP_LAST3 76 I have these defined in my header file.Instead can I define them in an array? – crr Jul 22 '12 at 03:04
  • @crr, Let's say you have 10 of them starting from 100: `std::array IDM_POPUP_LAST; std::iota (std::begin (IDM_POPUP_LAST), std::end (IDM_POPUP_LAST), 100);` should work. Then you can use `IDM_POPUP_LAST[5]` to mean 105. Is that about what you're looking for? – chris Jul 22 '12 at 03:06
  • @chris do we declare all these in header? I get an error "std has no member iota" , "array"?Also IDM_POPUP_LAST is undefined – crr Jul 22 '12 at 03:14
  • I see. If that's what you want, I guess I'll add an '03 version for those who can't use '11. – chris Jul 22 '12 at 03:16
  • is there some header file that needs to be included to solve this error? – crr Jul 22 '12 at 03:24
  • @crr, Yes, both `array` and `iota` require C++11, and headers. I made my answer include both methods, but I'll add the headers. – chris Jul 22 '12 at 03:27

1 Answers1

0

All you need is an array. You can get almost the same syntax without using ugly defines:

C++11

#include <algorithm> //for std::iota
#include <array> //for std::array

//Here's an array of 10 ints, consider changing the name now
std::array<int, 10> IDM_POPUP_LAST;

//This fills the array with incrementing values, starting at 100
std::iota (std::begin (IDM_POPUP_LAST), std::end (IDM_POPUP_LAST), 100);

//Now we can use them almost like before
appendToMenu (/*...*/, IDM_POPUP_LAST[3]); //uses value 103

//We can also loop through all of the elements and add them
for (const int i : IDM_POPUP_LAST) {
    AddToLast (i);
}

C++03

#include <algorithm> // for std::for_each
#include <vector> //for std::vector

std::vector<int> IDM_POPUP_LAST (10); //make array

for (std::vector<int>::size_type i = 0; i < IDM_POPUP_LAST.size(); ++i) //loop 
    IDM_POPUP_LAST [i] = i + 100; //assign each element 100, 101, etc.

appendToMenu (/*...*/, IDM_POPUP_LAST[3]); //use an element

std::for_each (IDM_POPUP_LAST.begin(); IDM_POPUP_LAST.end(); AddToLast); //add
chris
  • 60,560
  • 13
  • 143
  • 205
  • I have this defined in .cpp ON_COMMAND_RANGE(IDM_POPUP_LAST1, IDM_POPUP_LAST5, AddToLast). how can this be adjusted with using the array? – crr Jul 22 '12 at 03:35
  • @crr, The last parts of each should now loop through the array, passing `AddToLast` that resource's ID (that element's value). Note the new header required for `std::for_each`. For it to work like this, `AddToLast` needs to have one parameter: the ID. – chris Jul 22 '12 at 03:41
  • @crr, You most likely are not using C++11. Depending on your compiler, adding the option `-std=c++0x` or `-std=c++11` might work, and is preferable to using C++03, but if C++11 is not an option, the C++03 solution should still work. – chris Jul 22 '12 at 03:49
  • I'm developing in Visual Studio 10..what changes need to be made? – crr Jul 22 '12 at 04:27
  • @crr, I don't use VS, so I'm not too well-versed with changing things on there. The second code block should work fine regardless. – chris Jul 22 '12 at 04:40
  • when I define the for loop,for(int i = 0; i < IDM_POPUP_LAST.size(); ++i) IDM_POPUP_LAST [i] = i + 100; I get the error that for "FOR", it expected a declaration. Is there any specific header file? I'm not sure, I have all the general headers. – crr Jul 24 '12 at 21:16
  • @crr, Did you type `FOR` or `for`? `for` loops use lowercase letters: `for (int i = 0; i < 10; ++i)` is an example. – chris Jul 24 '12 at 21:21
  • @crr, That loop should be fine. All it need is ``, and `IDM_POPUP_LAST` to be declared. Perhaps this might be better in a new question. – chris Jul 24 '12 at 21:34
  • vector IDM_POPUP_LAST (5); for(int i = 0; i < IDM_POPUP_LAST.size(); ++i) IDM_POPUP_LAST [i] = i + 90 is what I have defined in the .h file. I stilll get the same error that for "for" it needs a declaration. – crr Jul 24 '12 at 22:00
  • Are you possibly missing a semicolon in `IDM_POPUP_LAST[i] = i + 90;`? – chris Jul 24 '12 at 22:05
  • nope..But I guess it is unable to associate to the C++ for loop. All the other definitions within that file have a "# define ABC".DO I have to define it in any such way? – crr Jul 24 '12 at 22:09
  • @crr, This would be a lot easier to answer with more code and context. I think it would make for a perfectly fine new question if you post all of the related code. – chris Jul 24 '12 at 22:37