c++c++11function-pointersundefined-reference

Error: `undefined reference` to function with array and function pointer as parameters


Each time I call the force function I get an undefined reference; collect2: error: ld returned 1 exit status.

Here is the declaration of the force function above the main function in main.cpp:

void force(Particle(&a_p)[SIZE], void(*force_func)(Particle&, Particle&));

Here is the main function in main.cpp:

int main() {

    Particle a_particle[SIZE];
    
    for (int i = 0; i++ < iterations;) {
        force(a_particle, float3::graviton);
        force(a_particle, float3::photon);
    }
    
    return 0;
}

Here is the definition of the force function below the main function in main.cpp:

void force(Particle(&a_p)[SIZE], void (*force_func)(const Particle&, const Particle&)) {
    for (unsigned int i = 0    ; i++ < SIZE - 1;)
    for (unsigned int j = i + 1; j++ < SIZE;    ) {
        force_func(a_p[i], a_p[j]);
    }
}

SIZE is a definition used for the size of the a_particle array and it's uses (Mine is set to 10).

Both float3 and Particle are structs declared in separate header files.

I tried initializing the function pointers separately as variables and passing them onto the function but that also did not work. Could anyone help me find why I still get this error?

If I am missing any required information I am happy to give more.


Solution

  •     // main.cpp
    
        #include "float3.h" // Include header file where float3 struct is declared
        #include "Particle.h" // Include header file where Particle struct is declared
    
        #define SIZE 10 // Define the size of the a_particle array
    
        void force(Particle(&a_p)[SIZE], void(*force_func)(const Particle&, const Particle&));
    
        int main() {
        Particle a_particle[SIZE];
        
        // Assuming iterations is defined somewhere
        int iterations = 10; // For example
    
        for (int i = 0; i++ < iterations;) {
            force(a_particle, float3::graviton);
            force(a_particle, float3::photon);
        }
        
        return 0;
    }
    
    void force(Particle(&a_p)[SIZE], void (*force_func)(const Particle&, const Particle&)) {
        for (unsigned int i = 0; i < SIZE - 1; ++i)
            for (unsigned int j = i + 1; j < SIZE; ++j) {
                force_func(a_p[i], a_p[j]);
            }
    }