c++ - Error in passing a map by reference to a function -
i want pass map reference function. here code:
void test(map<int, double *> &a); int main(){ map<int, double *> a; test(a); cout << a[1][1] << endl; return 0; } void test(map<int, double*> &a) { double red[] = {1.1, 2, 3}; a[1] = red; }
the problem that, a[1][1] supposed 2. however, when execute program, gives big number 1.73e120 !!!
double red[] = {1.1, 2, 3};
has automatic storage duration. means when function returns no longer valid.
you should this
void test(map<int, double*> &a) { double *red = new double[3]; // fill red. a[1] = red; }
Comments
Post a Comment