1

I'm fairly new to C++, so I'm not sure what I'm doing wrong.

This is my construct:

Struct

template<size_t N> struct Offsets 
{ 
    static const int length = N;
    DWORD offsets[N]; 
};

And the property:

template <size_t N>
std::map<std::string, std::map<DWORD, Offsets<N>>> pointers;

This results in a

Compiler Error C1001.

Whats wrong with that?

zx485
  • 28,498
  • 28
  • 50
  • 59
Dude
  • 59
  • 1
  • 5

2 Answers2

3

Variables can't be templated, they have to fully specified. So to declare your pointers variable you must specify the N.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • That sounds reasonable. thank you for the answer. Would you say using std::vector would be a better alternative? – Dude May 20 '13 at 12:08
  • @user2081200 I really can't say what would be best, as I don't really know what you want to accomplish. – Some programmer dude May 20 '13 at 12:09
  • Just a 3 dimensional mapping. like this: 1. Label, 2. Address, 3. Offsets. Offsets are not the same size, they vary. – Dude May 20 '13 at 12:10
  • @user2081200 Templates have to be fully evaluated at compilation, and it seems you want yo be able to have different sizes that are set during runtime. So yes, then a `std::vector` seems like a better solution. – Some programmer dude May 20 '13 at 12:14
  • @user2081200 It seems, you just want template/alias typedefs to use an arbitrary template instance of `struct Offsets` with the `std::map` template. This kind of typedef is already specified, but not yet supported everywhere: http://stackoverflow.com/q/26151/1175253 (a workaround was just posted by DaBrain) – Sam May 20 '13 at 12:18
1

You can't use a template on a variable. If you want to keep pointers flexible encapsule it in a template class or struct.

template< size_t N >
class PointerOffsetMap
{
...
public:
    std::map<std::string, std::map<DWORD, Offsets<N>>> pointers;
}

just a very simple example, you should probably make pointers private and add some access functions to get a nice interface.

DaBrain
  • 3,065
  • 4
  • 21
  • 28