STL Map insertion

I have a problem with inserting a new value into an STL map. My map takes an integer value as the key and an array of structs as the value. I have included the initialization of my map as well as the line of code where I'm trying to insert a new map value. Any help would really be appreciated.

1
2
3
4
5
6
7
8
9
//initializing a new map
map<int, cacheStruct[2]> cacheMap2Way;  
.
.
.
.
//attempt to insert a new value into the map (array2 is a struct array)
cacheMap2Way.insert(pair<int, cacheStruct[2]> (blockIndex, array2)); 


The error I get from VS 2010 is the following:


1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(163): error C2536: 'std::_Pair_base<_Ty1,_Ty2>::std::_Pair_base<_Ty1,_Ty2>::second' : cannot specify explicit initializer for arrays
1> with
1> [
1> _Ty1=int,
1> _Ty2=cacheStruct [2]
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(167) : see declaration of 'std::_Pair_base<_Ty1,_Ty2>::second'
1> with
1> [
1> _Ty1=int,
1> _Ty2=cacheStruct [2]
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(247) : see reference to function template instantiation 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base<int&,cacheStruct(&)[2]>(_Other1,_Other2)' being compiled
1> with
1> [
1> _Ty1=int,
1> _Ty2=cacheStruct [2],
1> _Other1=int &,
1> _Other2=cacheStruct (&)[2]
1> ]
1> c:\users\emory\dropbox\classes\ece2500\project3\project3\project3\main.cpp(227) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<int&,cacheStruct(&)[2]>(_Other1,_Other2)' being compiled
1> with
1> [
1> _Ty1=int,
1> _Ty2=cacheStruct [2],
1> _Other1=int &,
1> _Other2=cacheStruct (&)[2]
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.97
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Encapsulate it inside another struct like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <map>

struct cacheStruct {
  char c;
};

struct cacheArray {
  struct cacheStruct a[2];
};

using namespace std;

main()
{
  int blockIndex = 0;
  cacheArray array;

  map<int, cacheArray> cacheMap2Way;  
  cacheMap2Way.insert(pair<int, cacheArray> (blockIndex, array)); 
}

What is cacheStruct? a type or a variable name?

Is the error on the initialization line or insertion line?

Doesn't cacheMap2Way[blockIndex] = array2 work either?

There might be an error for the same reason as this code won't do what you want !
1
2
3
int tab1[2];
int tab2[2]={3,3};
tab1 = tab2;

The compiler won't try to copy the array 2 in the other, but try to assign the adress pointed by tab2 to replace the address pointed by tab1. And i'm not sure that would compile since tab1 isn't just a pointer but a fixed size array.
You might have to somehow insert the new item uninitialized and then memcpy what you want inside
cacheStruct is a struct...also [blockIndex] = array2 doesn't work (it gives me the error "must be modifiable lvalue")....I'm gonna try kfmfe04's suggestion when I get a chance
Topic archived. No new replies allowed.