0

I tried to call the function "calculate_distance" from the file calculus.cpp in my main program "calculator.cpp". But whenever I do that, I get the error "undefined reference to calculate_distance".

The function uses call by reference, and I don't know if that has something to do with the error, or if I imported something wrong. Maybe I messed something with the reference up?

Could someone please help me with my code?

calculus.cpp:

#include "calculus.h"
#include <cmath>

int calculate_distance(double a, double v_max, double t, double& l, double& v_end) {

    l = 0;
    v_end = 0;

    l = 0.5 * a * pow(t, 2);
    v_end = sqrt(2 * a * l);
    
    return 0;
}

calculus.h:

#ifndef CALCULUS_H
#define CALCULUS_H

int calculate_distance(double a, double v_max, double t, double& l, double& v_end);

#endif

calculator.cpp:

#include <iostream>
#include "/workspaces/20230324_sew_ex03_functions-Schwotz_Scherko/src/libraries/Calculus/calculus.h"

using std::cout;
using std::cin;
using std::endl;

double acceleration = 0;
double maximum_velocity = 0;
double time_length = 0;
double length = 0;
double velocity_end = 0;

int main() {
    cout << "Please input the uniform acceleration a: ";
    cin >> acceleration;
    cout << "Please input the maximum velocity v_max: ";
    cin >> maximum_velocity;
    cout << "Please enter the time t: ";
    cin >> time_length;

    calculate_distance(acceleration, maximum_velocity, time_length, length, velocity_end);

    cout << "--------------------------------------------------------------------------------" << endl;
    cout << "INPUTS:" << endl;
    cout << "--------------------------------------------------------------------------------" << endl;
    cout << "a=     " << acceleration << endl;
    cout << "v_max= " << maximum_velocity << endl;
    cout << "t=     " << time_length << endl;
    cout << "--------------------------------------------------------------------------------" << endl;
    cout << "RESULTS:" << endl;
    cout << "--------------------------------------------------------------------------------" << endl;
    cout << "l=     " << length << endl;
    cout << "v_end= " << velocity_end << endl;
    cout << "--------------------------------------------------------------------------------" << endl;
    cout << "Quit? [y/n]: ";


}

schwitzky
  • 3
  • 3
  • 1
    How do you compile/link it? (commands?) – teapot418 Mar 30 '23 at 08:22
  • 1
    Based on experience on this site, **almost certainly** the problem is that you are failing to compile `calculus.cpp`. This often happens when beginners move from programs that have only one cpp file to programs that have more than one. The build process that worked for a single cpp file no longer works for multiple cpp files. – john Mar 30 '23 at 08:31
  • So to fix this problem you need to say a little about how you compile your code. – john Mar 30 '23 at 08:33
  • Incidentally what is the point of assigning twice to the variables `l` and `v_end` in `calculate_distance`. You can remove the initial assignments to zero, they add nothing to the code. – john Mar 30 '23 at 08:34

0 Answers0