-3

The following is the code I've written in an implementation file for a function that calculates median of a double vector :

#include<algorithm>
#include<stdexcept>
#include<vector>
#include "median.h"
using std::domain_error;
using std::vector;

double median(vector<double> vect)
{
    typedef vector<double>::size_type vec_sz;
    vec_sz size=vect.size();
    if(size==0)
        throw domain_error("median of an empty vector");
    sort(vect.begin(),vect.end());
    vec_sz mid= size/2;
    return size%2==0 ? (vect[mid]+vect[mid-1])/2 : vect[mid];
}

I get the following when I compile it using g++ median.cpp on the command line:

/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    It returned 1 because linking failed. – chris Jan 05 '18 at 03:38
  • What is that? And, how do I coreect it? – lakshay.angrish Jan 05 '18 at 03:38
  • 4
    It's the error you posted: *undefined reference to main*. You don't have a `main` function. – chris Jan 05 '18 at 03:39
  • Please show the command line you used to compile and link your program. – jww Jan 05 '18 at 03:40
  • @jww I used the linux command line – lakshay.angrish Jan 05 '18 at 03:40
  • @chris I saw this from Accelerated C++ and there is no main function inclusion in their program – lakshay.angrish Jan 05 '18 at 03:41
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Jan 05 '18 at 03:42
  • @lakshay.angrish, Then it's presumably meant to be compiled separately and linked with something else that does have a `main` function. The book should go over how to build its code. – chris Jan 05 '18 at 03:47
  • @chris The book says that by defining functions in seperate files, we can compile the functions seperately and before our main program. And, I actually tried to include this function implementation's header file in a test program with a main function, but that still gives the same error. – lakshay.angrish Jan 05 '18 at 03:51
  • undefined reference to `main' – Li Kui Jan 05 '18 at 03:57
  • Possible duplicate of [How do I link object files in C? Fails with "Undefined symbols for architecture x86\_64"](https://stackoverflow.com/questions/15441877/how-do-i-link-object-files-in-c-fails-with-undefined-symbols-for-architecture) – jww Jan 05 '18 at 04:05

1 Answers1

0

The error is because you don't have a main function. Create a main function and call the median function from main function.

#include<algorithm>
#include<stdexcept>
#include<vector>
#include "median.h"
using std::domain_error;
using std::vector;

double median(vector<double> vect)
{
    typedef vector<double>::size_type vec_sz;
    vec_sz size=vect.size();
    if(size==0)
        throw domain_error("median of an empty vector");
    sort(vect.begin(),vect.end());
    vec_sz mid= size/2;
    return size%2==0 ? (vect[mid]+vect[mid-1])/2 : vect[mid];
}

void main()
{
   // call the median(vector<double> vect) function from here
    vector<double> vect = {10,20,30}; // initialize with numbers 
    double retval = median(vect);
}
Akhil V Suku
  • 870
  • 2
  • 13
  • 34