-1

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.

5
  • 3
    We need a minimal reproducible example Commented Apr 27, 2024 at 4:47
  • This is a linker error. How do you build your project. What commands do you use? Commented Apr 27, 2024 at 4:48
  • Take a careful look at the const keyword. Just copy & paste and compare the function parameters on neighbor lines. Commented Apr 27, 2024 at 4:50
  • Instead of three code blocks (meaning three copy-pastes to reproduce your result), you should put the code from main.cpp in one code block. Commented Apr 27, 2024 at 5:21
  • Questions are for questions, not answers. If you want to answer your own question, the answer belongs in an answer. (If you just want to indicate that you have received an acceptable answer, accepting that answer -- as you have done -- does that.) Commented Apr 27, 2024 at 5:24

1 Answer 1

0
    // 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]);
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Code-only answers are rarely useful in the long run. Answers should have text explaining what to look for in the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.