Hi,

I'm trying to find the resultant of a given data using fstream. I made the program and everything and I believe its all correct however, when I try to run it, I'm getting a linker error saying "undefined reference to Resultant(const double...)" which I can't figure out how to fix it. Can you please help me spot the problem?


Thank you in advance.


This is the program:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>

using namespace std;

void Resultant(const double[], const double[], const double[], const double[], const int,
                double&, double&, double&);

const double PI=acos(-1.0);
int main()
{
    const int size = 200 ;
    int moves = 0;
    double Magnitude[size], X_axis[size], Y_axis[size], Z_axis[size];
    double X_Resultant=0, Y_Resultant=0, Z_Resultant=0;
    string choice, answer;
    
    cout << "This program will calculate the Resultant Forces from a "
         << "selected data sheet.";
         
        do 
        {      
    cout << "\n\nPlease type in the data sheet you want to be calculated?"
         << "\nEnter Force1 or Force2 : " ;
    cin >> choice;
                
    if (choice == "Force1" || "force1")
    {
                        ifstream Data("Forces1.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open\nCheck File" << endl;
 
           for ( int i=0; !Data.eof();i++)
               {
                  Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
                   moves++;
                                   }     
                     Data.close();   break;
     }
    
    else if (choice == "Force2" || "force1")
    {
                        ifstream Data("Forces2.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open!\nCheck File" << endl;
 
                  for ( int i=0; !Data.eof();i++)
                  {
        Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
        moves++;
                                   }     
        Data.close(); break;
     }
     
            else {
                     cout << "\nERROR! Invalid Input! \nWould you like to"
                          << " try again? Enter y or n : " ;
                      cin >> answer;
                          }
}
     while (answer == "y" || answer == "Y");                   
     
     Resultant(Magnitude, X_axis, Y_axis, Z_axis, moves, X_Resultant, Y_Resultant, Z_Resultant);
     
     cout << setw(6) << X_Resultant << setw(6) << Y_Resultant << setw(6) << Z_Resultant;
     
system("pause");
return 0;
}

void Resultant(const double M[], const double X[], const double Y[], double Z[],
const int moves, double& Rx, double& Ry, double& Rz)
 {
      double Fx[moves], Fy[moves],Fz[moves];
        for (int i=0; i<3; i++) // using 3 to test the program
        {
        Fx[i]= M[i]*cos(X[i]*180/PI);
        Fy[i]= M[i]*cos(Y[i]*180/PI);
        Fz[i]= M[i]*cos(Z[i]*180/PI);
        }

        for (int i=0; i<3; i++)
        {
            Rx += Fx[i];
            Ry += Fy[i];
            Rz += Fz[i];
        }
        
 }

Dani AI

Generated

The linker error was caused by the function-prototype/definition mismatch that and spotted: the declared signature in main does not exactly match the defined signature, so the compiler emits a reference to one mangled name while the linker can't find a definition for it. Make the declaration and the definition identical (same parameter types, same const qualifiers and linkage) and recompile.

A short checklist for "undefined reference" problems:

  • Ensure the prototype and definition exactly match (cv-qualifiers like const on parameter types matter).
  • Confirm the definition is compiled and linked into the final binary (all .o/.cpp files included).
  • Watch for different linkage (e.g., extern "C" vs C++), namespaces, or member vs free functions.
  • Turn on warnings (-Wall -Wextra for gcc/clang, enable MSVC warnings) and, if needed, inspect object-file symbols with nm/c++filt to see what symbol the linker is missing.

Other issues in the posted code worth fixing now:

  • Cosine expects radians. Convert degrees to radians with angle * PI / 180.0 (the original 180/PI is the inverse).
  • Variable-length arrays like Fx[moves] are non-standard in C++; prefer std::vector<double> or reserve and push values.
  • Avoid for(...; !Data.eof(); ...) for file reads. Use a read-while loop such as while (Data >> a >> b >> c >> d) to avoid partial/invalid reads or buffer overruns.

A clearer design: store each force in a small struct and pass a std::vector of those to a single function that returns the resultant. Example pattern (illustrative):

struct Force { double magnitude, degX, degY, degZ; };

Force compute_resultant(const std::vector<Force>& forces) {
    Force R{0,0,0,0}; // use R.degX/R.degY/R.degZ as Rx,Ry,Rz
    for (const auto &f : forces) {
        double rx = f.magnitude * cos(f.degX * PI / 180.0);
        // accumulate rx, ry, rz into R.degX/R.degY/R.degZ
    }
    return R;
}

The signature fix noted by / and the string-comparison fix pointed out by are the immediate fixes; the design suggestions above improve safety and maintainability going forward.

Recommended Answers

All 8 Replies

Declaration

void Resultant(const double[], const double[], const double[], [B]const[/B] double[], const int, double&, double&, double&);

does not match the definition

void Resultant(const double M[], const double X[], const double Y[], [B]/* ??? */[/B] double Z[], const int moves, double& Rx, double& Ry, double& Rz)
{
   // ...

Not a great idea, writing a function with eight(!) parameters.

check the implementation of your Resultant function. You forgot the const on Z[].

vij beat me to it!

wow you guys are right. silly me. haha. what is the best way to pass 8 variables to a function? just curious.

thank you for finding my mistake.

Well on the top of my head I have 2 "solutions" for passing less variables.

1 defining the variables global
or passing a vector of doubles and a vector of arrays of doubles.

Take this with a big grain of salt and wait for a competent coder with his opinion. I just try to get some opinions as well (because on another project I work on I pass alot of variables as well).

i see but this project is recording numbers from a file thats why im passing a lot of variables. hehe. your right though it's unsafe.

i have a problem with my program :( . i don't know how or what happen but suddenly it doesn't go to "else" statement saying "try again" if i type in random words, it just goes to the first "if". my "else if" and "if" statements records the right file when i type in the correct word phrase. any idea why this is happening?

this is the do while loop:

do{      
                        
    cout << "\n\nPlease type in the data sheet you want to be calculated?"
         << "\nEnter Forces1 or Forces2 : " ;
    cin >> file;
    
      
    if (file == "Forces1" || "forces1")
    {
                        ifstream Data("Forces1.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open! Check File If Created" << endl; 
                 
           for ( int i=0; !Data.eof();i++)
               {
                  Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
                   moves++;
                                   }     
                     Data.close(); 
     }
    
    else if (file == "Forces2" || "forces1")
    {
                        ifstream Data("Forces2.DAT");
 
                                  if (Data.fail())
                                     cout <<"\nFail To Open! Check File If Created" << endl; 
 
                  for ( int i=0; !Data.eof();i++)
                  {
        Data >> Magnitude[i] >> X_axis[i] >> Y_axis[i] >> Z_axis[i];
        moves++;
                                   }     
        Data.close(); 
     }
     
            else {
                     cout << "\nERROR! Invalid Input! \nWould you like to"
                          << " try again? Enter y or n : " ;
                      cin >> answer;
                          }
}
     while (answer == "y" || answer == "Y");
if (file == "Forces1" || "forces1")

must be

if(file == "Forces1" || file == "forces1")

otherwise, in the first instance it's evaluating "forces1" as true since it is not null, so your statement's always true.

Same thing for the else if condition, too.

thank you for pointing that out. i got it to work because of you guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.