2010-04-06 14 views
21

göremiyorum ve ben çalıştığımda değeri, bir diziharitası değeri olarak diziyi kullanarak: Ben anahtar bir int bir harita oluşturmak için çalışıyorum hata

int red[3] = {1,0,0}; 
int green[3] = {0,1,0}; 
int blue[3]  = {0,0,1}; 

    std::map<int, int[3]> colours; 

colours.insert(std::pair<int,int[3]>(GLUT_LEFT_BUTTON,red)); //THIS IS LINE 24 ! 
colours.insert(std::pair<int,int[3]>(GLUT_MIDDLE_BUTTON,blue)); 
colours.insert(std::pair<int,int[3]>(GLUT_RIGHT_BUTTON,green)); 
Ancak

olduğunu Bu kodu derledim, aşağıdaki hatayı alıyorum.

g++ (Ubuntu 4.4.1-4ubuntu8) 4.4.1 

In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66, 
       from /usr/include/c++/4.4/bits/stl_tree.h:62, 
       from /usr/include/c++/4.4/map:60, 
       from ../src/utils.cpp:9: 
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = int, _T2 = int [3]]’: 
../src/utils.cpp:24: instantiated from here 
/usr/include/c++/4.4/bits/stl_pair.h:84: error: array used as initializer 
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = int, _U2 = int [3], _T1 = const int, _T2 = int [3]]’: 
../src/utils.cpp:24: instantiated from here 
/usr/include/c++/4.4/bits/stl_pair.h:101: error: array used as initializer 
In file included from /usr/include/c++/4.4/map:61, 
       from ../src/utils.cpp:9: 
/usr/include/c++/4.4/bits/stl_map.h: In member function ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = int, _Tp = int [3], _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, int [3]> >]’: 
../src/utils.cpp:30: instantiated from here 
/usr/include/c++/4.4/bits/stl_map.h:450: error: conversion from ‘int’ to non-scalar type ‘int [3]’ requested 
make: *** [src/utils.o] Error 1 

Hatanın nerede olduğunu gerçekten göremiyorum. Ya da bir hata olsa bile.

+0

her şey öğrenin gün :) – Tom

cevap

33

Dizileri bu değere göre kopyalayamazsınız.

İşte birkaç çözümler vardır, ama sizin ihtiyaçlarınız için # 4 tavsiye:

1) yerine bir dizi

2) 3 elemanların diziler için işaretçiler haritası kullanabilirsiniz bir std::vector kullanın.

int red[3] = {1,0,0}; 
int green[3] = {0,1,0}; 
int blue[3]  = {0,0,1}; 
std::map<int,int(*)[3]> colours; 
colours.insert(std::pair<int,int(*)[3]>(GLUT_LEFT_BUTTON,&red)); 
colours.insert(std::pair<int,int(*)[3]>(GLUT_MIDDLE_BUTTON,&blue)); 
colours.insert(std::pair<int,int(*)[3]>(GLUT_RIGHT_BUTTON,&green)); 
//Watch out for scope here, you may need to create the arrays on the heap. 

3) 3 elemanlarının boost tuples yerine dizileri kullanın.

4) Bir dizi kullanmak yerine 3 öğe alan yeni bir yapı oluşturun. Haritayı yap. Veya dizinizi bir yapıya sarın ve bu da işe yarayacaktır.

struct Triple 
{ 
    int color[3]; 
}; 

//Later in code 
Tripple red = {1, 0, 0}, green = {0, 1, 0}, blue = {0, 0, 1}; 
std::map<int,Triple> colours; 
colours.insert(std::pair<int,Triple>(GLUT_LEFT_BUTTON,red)); 
colours.insert(std::pair<int,Triple>(GLUT_MIDDLE_BUTTON,blue)); 
colours.insert(std::pair<int,Triple>(GLUT_RIGHT_BUTTON,green)); 
+0

Ayrıca, güzel alternatif bir çözüm. – Tom

+0

yaklaşık 3) Bu küçük bir proje, destek artıramaz, ama bir göz atacağız, teşekkürler. – Tom

+0

@ Tom: En kolay çözümünüz olan 4) eklendi. –

6

Diziler C++ 'da birinci sınıf yapılar değildir. std::map değerleri için Copy Constructible veya Assignable değildir. boost::array veya std::vector'u kullanabilirsiniz.

1

Diziler standart kapta saklanan veriler olamaz (std::pair)

3

Diğer bir alternatif bir sargı yapı içinde diziler koymaktır:

struct Wrapper { int value[3]; }; 

    // ... 
    Wrapper red = {{1,0,0}};  
    std::map<int,Wrapper> colours;  
    colours.insert(std::pair<int,Wrapper>(1, red)); 
0

kullanımı std :: make_pair:

int red[3] = {1,0,0}; 
int green[3] = {0,1,0}; 
int blue[3]  = {0,0,1}; 
std::map<int,int(*)[3]> colours; 
colours.insert(std::make_pair(GLUT_LEFT_BUTTON,&red)); 
colours.insert(std::make_pair(GLUT_MIDDLE_BUTTON,&blue)); 
colours.insert(std::make_pair(GLUT_RIGHT_BUTTON,&green)); 
5

std::tr1::array'u kullanın.

typedef std::tr1::array<int, 3> Triple; 
Triple red = {1, 0, 0}; 
Triple green = {0, 1, 0}; 
Triple blue = {0, 0, 1}; 
std::map<int, Triple> colours; 
colours.insert(std::make_pair(GLUT_LEFT_BUTTON, red)); 
colours.insert(std::make_pair(GLUT_MIDDLE_BUTTON, blue)); 
colours.insert(std::make_pair(GLUT_RIGHT_BUTTON, green)); 
+1

da şu anda sadece 'std :: array' ;-) olarak bilinen bu gerçekten burada sadece hala ilgili cevap yapmak. Bu kadar az oyu nasıl tuhaf ... –

5

yerine, int [] eşleme * int eşleme etmeyin böyle:

#include <iostream> 
#include <map> 
using namespace std; 
int main(){ 
    std::map<int,int*> colors; 
    int red[] = {3,7,9}; 
    colors[52] = red; 
    cout << colors[52][1]; //prints 7 
    colors[52][1] = 11; 
    cout << colors[52][1]; //prints 11 
    return 0; 
} 
0

Yaklaşım C yapısını kullanarak ++

int MAX_DATA_PER_INSTR = 8; 
//struct to hold the values. remember to write the constructor 
struct InstrChar 
{ 
    InstrChar(int in[MAX_DATA_PER_INSTR]) { 
    //c alternative is memcopy 
    std::copy(in, in+MAX_DATA_PER_INSTR, data); 
    } 
    int data[MAX_DATA_PER_INSTR]; 
}; 

// create a key value pair 
std::map <int, InstrChar> address_instructions; 
std::map <int, InstrChar>::iterator it; 

// sample array, 8 elements 
int xp[MAX_DATA_PER_INSTR ] = {31,4,3,4,4,3,1,2}; 
address_instructions.insert(std::pair<int, InstrChar>(PC, xp)); 
it = address_instructions.find(PC); 
InstrChar buf1 = it->second; 
//integer pointer to the array, can be dereferenced as *p, *(p+1), .... //*(p+7) 
int *p = buf1.data; 

//in case you need to print these values out. They can also be referred to as buf1.data[0], buf1.data[1], buf1.data[2] 
printf("%d\n", (*p)); 
printf("%d\n", *(p+1)); 
printf("%d\n", *(p+2)); 
printf("%d\n", *(p+3)); 
printf("%d\n", *(p+4)); 
printf("%d\n", *(p+5)); 
printf("%d\n", *(p+6)); 
printf("%d\n", *(p+7)); 
İlgili konular