0
votes

Could anyone help me solve the error shown in the code below?

#include <iostream>
#include "FG.h"

struct pr { double (*fG[3]) (double, double, double, double*);};

int main()
{       


    double (*fG[3]) (double, double, double, double*);

    fG[0] = GX00;
    fG[1] = GX00;
    fG[2] = GX22;

    double prx[2] = {10, 1};
    struct pr params ={ fG };
    std::cout << params.fG[0]( 1 , 0.5 , 1, prx ) << std::endl;

    return 0;
}

compile:

$ g++ -c test.cpp 

test.cpp: In function ‘int main()’: test.cpp:15:25: error: array must be initialized with a brace-enclosed initializer

2
Welcome to SO. You should take the tour. Glad your problem was resolved.Steve P.

2 Answers

4
votes

You can't initialize one array from another in C (just like array assignment doesn't work). Try this instead:

struct pr params = { { &GX00, &GX00, &GX22 } };

Here's another way, using a custom constructor: http://ideone.com/blVBox

0
votes

The problem is, that "reading" fG (as you do in struct pr params ={ fG };) won't give you a copy of the array but a copy of the first address of the array. It's comparable to the following Situation:

char  buffer[200] = "Test";
char *p = buffer;

In this Situation p stores only the first address of the buffer. To initialize the struct on the other Hand, you need to tell the Compiler for every element what should be stored. So the correct initialization for your struct is:

struct pr params ={ 
    { GX00, GX00, GX22 }
};

Further I'd recommend you typedef the function signature instead of replicatiing it over and over again. It makes the code much more readable:

typedef double (*tCallback) (double, double, double, double*);

struct pr {
    tCallback fG[3];
}