Question:
Help with "undefined reference error" in c++?
anonymous
2010-11-27 12:23:25 UTC
I am trying to run a program that outputs the largest numbe between 4 numbers. When i run the program i get this error " [Linker error] undefined reference to `larger(double, double, double, double)' " and " ld returned 1 exit status " . What am i doing wrong and what do i do to fix this. Thanks in advance.

program so far:

[code]
#include
#include
#include
using namespace std;

double larger(double x1, double x2, double y1, double y2);


int main(int argc, char *argv[])
{


double x1,x2,y1,y2;



cout<<"Enter x1"< cin>>x1;
cout<<"Enter x2"< cin>>x2;

cout<<"Enter y1"< cin>>y1;
cout<<"Enter y2"< cin>>y2;
cout<

cout<<"The larger between x1,y1 ,x2,y2 is "< cout< return 0;

system("PAUSE");
return EXIT_SUCCESS;
}

[/code]
Four answers:
SĂ­dhe
2010-11-27 12:33:38 UTC
You declared the function larger(double, double, double, double), but you never actually implemented it.



The code compiled, but failed to link because it couldn't find the larger() function implementation in any of your object files.



UPDATE: No, it is being CALLED there.



Where is the code that makes larger() actually *DO* something?



Try adding this code to the bottom:



double larger(double x1, double x2, double y1, double y2) {

double largest = x1;

if (x2 > largest) largest = x2;

if (y1 > largest) largest = y1;

if (y2 > largest) largest = y2;

return largest;

}



SIDE NOTE:

return 0;

// NOTHING WILL OCCUR AFTER THIS

// BECAUSE YOU RETURNED

// (Surprised you didn't get a warning)



system("PAUSE");

return EXIT_SUCCESS;
green meklar
2010-11-27 15:58:46 UTC
Right now, larger() has not been defined. You have declared it, but you have not written out the function body. How is the computer supposed to know what to do on that function call if you haven't provided a function body? The computer can't read your mind, you need to tell it what to do.
The Phlebob
2010-11-27 12:40:05 UTC
You have to define (provide code for) the larger() function. All you've provided is its declaration (how to call it from other functions).



Hope that helps.
?
2016-05-31 09:44:19 UTC
There's no pow(..) function that takes an int as the base. That's the undefinded reference. change that int 2 to 2.0 and then it should work. Here's the various forms of the pow(..) function: pow double pow ( double base, double exponent ); long double pow ( long double base, long double exponent ); float pow ( float base, float exponent ); double pow ( double base, int exponent ); long double pow ( long double base, int exponent );


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...