can someone please help me with this code?
i am getting the following error : [Linker error] undefined reference to `enr(int, int, double, double, double)'

thanks

#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;

float ran1(long *);

#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define NTAB 32
#define NDIV (1+(IM-1)/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)

float ran1(long *idum)
{
        int j;
        long k;
        static long iy=0;
        static long iv[NTAB];
        float temp;

        if (*idum <= 0 || !iy) {
                if (-(*idum) < 1) *idum=1;
                else *idum = -(*idum);
                for (j=NTAB+7;j>=0;j--) {
                        k=(*idum)/IQ;
                        *idum=IA*(*idum-k*IQ)-IR*k;
                        if (*idum < 0) *idum += IM;
                        if (j < NTAB) iv[j] = *idum;
                }
                iy=iv[0];
        }
        k=(*idum)/IQ;
        *idum=IA*(*idum-k*IQ)-IR*k;
        if (*idum < 0) *idum += IM;
        j=iy/NDIV;
        iy=iv[j];
        iv[j] = *idum;
        if ((temp=AM*iy) > RNMX) return RNMX;
        else return temp;
}
#undef IA
#undef IM
#undef AM
#undef IQ
#undef IR
#undef NTAB
#undef NDIV
#undef EPS
#undef RNMX

//-------------------------------------------------------------------------------------

//--------------------------------------Defining Energy Function--------------------
double annint(double, double);

double annint (double xcoz, double box) //If xcoz is not between (-0.5 to 0.5 * box) it is taken as part of another box
{
    double co1, co2;
    if (xcoz > 0)
    {
          co1 = (int(xcoz/box))*box;
          return (xcoz-co1);
    }

    else if (xcoz < 0)
    {
         co2 = (int(xcoz/box))*box;
         return (xcoz-co2);
    }
    else
         return(0);
}

// The LJ parameters for carbon and oxygen molecule (united atom) are as follows:
// Carbon: sigma = 3.4   Angstrom, epsilon = 43.3 K (= 0.086 kcal/mol)
// Oxygen: sigma = 3.43 Angstrom, epsilon = 113 K  (= 0.22454 kcal/mol)


double enr(int, int, double, double, double);

double enr(int no, int nc, double box, double x[][3], double c[][3])
    {
               double uco, uoo, u; //uco = Interaction energy between C & O
          for (int b = 0; b < no; b++)
          {
              for (int d=0; d<nc; d++)
              {
                  double xcoz = x[b][2]-c[d][2];
                  xcoz = annint (xcoz, box);
                  double rco = sqrt(pow((x[b][0]-c[d][0]),2) + pow((x[b][1] - c[d][1]),2) + pow((xcoz),2));
                  uco = uco + (4 * 0.031) * (pow((3.415/rco),12) - pow((3.415/rco),6));
              }
          }
          for (int e=0; e<(no-1); e++)
          {
              for (int f=(e+1); f<no; f++)
              {
                  double xooz = x[e][2]-x[f][2];
                  xooz = annint (xooz, box);
                  double roo = sqrt(pow((x[e][0]-x[f][0]),2) + pow((x[e][1]-x[f][1]),2) + pow((xooz),2));
                  uoo = uoo + (4 * 0.22454) * (pow((3.43/roo),12) - pow((3.43/roo),6));
              }
          }
          u = uco + uoo;
          return(u);
    }


//--------------------------------------------------------------//

double x[1000][3];
double xn[3];
double temp[3];
double limit[3] = {6, 6, 20};
double c[1000][3];
long RAND = 3434534;
int nc, no, n;
double box = 24.6;

// x = coordinates of adsorbate
// temp = variable for storing 'new' coordinates of adsorbate
// temp  = variable for storing old coordinates of adsorbate if accepted
// limit = limit of dimentions where O atom can be inserted
// c = coordinates of absorbent


int main()
{   

    cout << "Insert Number of Oxygen Molecules To Be Inserted: ";
    cin >> no;

    cout << "CHECK 1\n";


//------------------------- Reading File --------------------------



    cout << "Insert number of MC moves: ";
    int z;
    cin >> z;

    cout << z;
    cout << "\nCHECK 2\n";

//-------------------------Finding Number of Carbons-----------------

    std::ifstream cnt ("CNT.txt",std::ios::in);

    if (cnt.is_open())
    cout << "CHECK 3\n";

    int itr = 0;
    nc = 0;

    char atm[] = "ATOM";    
    char atom[] = "ATOM";

    for (itr = 0; itr < 1000; itr++)
    {
        for (int p = 0; p < 4; p++)
        {
        cnt >> atom[p];
        }

  //      cout << '\t' << atom << '\t';                  //CHECK
  //              cout << atom;  

        if (strcmp(atm,atom) == 0)
           {
                nc++;
            //    cout << atom;                  //CHECK
           }
        cnt.ignore(10000,'\n');
    }


    cnt.close();
/*   
    cout << nc;
    cout << itr;

    if (cnt.is_open());
    else
    cout << "CHECK 4\n";

*/

    cout << "CHECK 5\n";


//---------------Insertion Co-ordinates of Carbon In Array----------------
    cnt.open ("CNT.txt",std::ios::in);

    for (int q = 0;q < 4;q++)
    cnt.ignore(10000,'\n');


    for (int s = 0;s < nc; s++)
    {
        cnt.ignore(33,'-');
        cnt >> c[s][0] >> c[s][1] >> c[s][2];
        cnt.ignore(10000,'\n');
        cout << c[s][0] << c[s][1] << c[s][2];
    }


    cnt.close();

    cout << "CHECK 6\n";


// ----------------- Inserting Oxygen atoms ------------------------
      int a, b;

        for (a = 0; a < 3; a++)
        {
            for (b = 0; b < no; b++)
            {
 //               cout << "\n" << x[a][b] << "\n";
                x[b][a] = (ran1(&RAND) - 0.5 ) * (limit[a]);
 //               cout << "\n" << x[a][b] << "\n";
            }
        }

        cout << "CHECK 7\n";

/* Repeat Translation Move 'z' Number of Times (User Specified) */     


int y;
for (y = 0; y < z; y++)
{

// --------------- Loop for performing 1 MC Move ------------------------------


    /* Randomly Select An Oxygen Molecule */
    // Oxygen Molecule = int (Rand(f) * No. of Oxygen) + 1

    n = (int (ran1(&RAND) * (no-1)));

    /* Randomly Generate New X,Y,Z Co-ordinates */
    //  x(new) = x(old) + ^x

    for (a = 0; a<3; a++)
    {
        xn[a] = x[n][a] + (ran1(&RAND) -0.5);
        temp[a] = x[n][a];
//        cout << n << '\t' << xn[a] << '\t' << temp[a] << '\t';
    }

    /* Call Old Energy, New Energy */

    double uo, un;

    uo = enr(no, nc, box, x[no][3], c[nc][3]);

    for (int g = 0; g < 3; g++)
        x[n][g] = xn[g];

    un = enr(no, nc, box, x[no][3], c[nc][3]);

    cout << '\n' << uo << '\t' << un << '\t';

    /* Find Out The Probability of Translation */

    double h, prob;

    h = 0.590195;
    prob = exp(-h*(un-uo));

//    cout << prob;

    /* Generate A Random Number (If Greater Than Probability Reject The Move Otherwise Accept It) */
    /* Rand(f);
       If Rand(f) < acc(o->n)
       x(o) = xn;
    */
    if (ran1(&RAND) > prob)
    {
               int i;
               for (i = 0; i<3; i++)
               x[n][i] = temp[i];
               cout << x[n][i];
    }
}

    cout << "CHECK 8\n";

//----------- Writing Co-ordinates of Oxygen in File------------------

    std::ofstream cnt2 ("CNT2.txt",std::ios::out);


    int num = nc;
    for (n=0; n<no; n++)
    cnt2 << "ATOM    " << num++ << "  O   UNK     1      " << x[n][0] << "  " << x[n][1] << "  " << x[n][2] << "  1.00  0.00/n";

    cnt2 << "END";
    cnt2.close();
    //---------------------- CHECK --------------------------------

    cout << "END\n";

    cout << c[1][1] << "\n" << c[4][1] << "\n" << c[7][1] << "\n";    //CHECK Some Random Co-ordinates
    cout << no << "\n";                                               //CHECK No. of Oxygen
    cout << nc << "\n";                                               //CHECK No. of Carbon
    cout << num << "\n";                                              //CHECK No. of Carbon + Oxygen
    cout << enr(no, nc, box, x[no][3], c[nc][3]);                             //CHECK Final Energy


    return (0);
}
    /* TODO (#1#): Not working  Check5-Check6 */
        /* TODO (#2#): Not working  Energy Function (Used From Check7-Check8) */
                /* TODO (#3#): Working  Till Check8 */

Edited 3 Years Ago by pyTony: fixed formatting

Here is the first line of the function you actually wrote:
double enr(int no, int nc, double box, double x[][3], double c[][3])

Here is what you thought you were trying to write, and what you're trying to call:
double enr(int, int, double, double, double);

double x[][3] is not the same as double
double c[][3] is not the same as double

To begin with, please use [code] tags around all code samples in the future, especially ones as large as this. This is very important, as the code tags force the software to retain the indentation; without them, the code is almost impossible to read.

#include <iostream>
#include <fstream>
#include <math.h>
#include <string>
using namespace std;

float ran1(long *);

#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define NTAB 32
#define NDIV (1+(IM-1)/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)

float ran1(long *idum)
{
    int j;
    long k;
    static long iy=0;
    static long iv[NTAB];
    float temp;

    if (*idum <= 0 || !iy)
    {
        if (-(*idum) < 1) *idum=1;
        else *idum = -(*idum);
        for (j=NTAB+7; j>=0; j--)
        {
            k=(*idum)/IQ;
            *idum=IA*(*idum-k*IQ)-IR*k;
            if (*idum < 0) *idum += IM;
            if (j < NTAB) iv[j] = *idum;
        }
        iy=iv[0];
    }
    k=(*idum)/IQ;
    *idum=IA*(*idum-k*IQ)-IR*k;
    if (*idum < 0) *idum += IM;
    j=iy/NDIV;
    iy=iv[j];
    iv[j] = *idum;
    if ((temp=AM*iy) > RNMX) return RNMX;
    else return temp;
}
#undef IA
#undef IM
#undef AM
#undef IQ
#undef IR
#undef NTAB
#undef NDIV
#undef EPS
#undef RNMX

//-------------------------------------------------------------------------------------

//--------------------------------------Defining Energy Function--------------------
double annint(double, double);

double annint (double xcoz, double box) //If xcoz is not between (-0.5 to 0.5 * box) it is taken as part of another box
{
    double co1, co2;
    if (xcoz > 0)
    {
        co1 = (int(xcoz/box))*box;
        return (xcoz-co1);
    }

    else if (xcoz < 0)
    {
        co2 = (int(xcoz/box))*box;
        return (xcoz-co2);
    }
    else
        return(0);
}

// The LJ parameters for carbon and oxygen molecule (united atom) are as follows:
// Carbon: sigma = 3.4   Angstrom, epsilon = 43.3 K (= 0.086 kcal/mol)
// Oxygen: sigma = 3.43 Angstrom, epsilon = 113 K  (= 0.22454 kcal/mol)


double enr(int, int, double, double, double);

double enr(int no, int nc, double box, double x[][3], double c[][3])
{
    double uco, uoo, u; //uco = Interaction energy between C & O
    for (int b = 0; b < no; b++)
    {
        for (int d=0; d<nc; d++)
        {
            double xcoz = x[b][2]-c[d][2];
            xcoz = annint (xcoz, box);
            double rco = sqrt(pow((x[b][0]-c[d][0]),2) + pow((x[b][1] - c[d][1]),2) + pow((xcoz),2));
            uco = uco + (4 * 0.031) * (pow((3.415/rco),12) - pow((3.415/rco),6));
        }
    }
    for (int e=0; e<(no-1); e++)
    {
        for (int f=(e+1); f<no; f++)
        {
            double xooz = x[e][2]-x[f][2];
            xooz = annint (xooz, box);
            double roo = sqrt(pow((x[e][0]-x[f][0]),2) + pow((x[e][1]-x[f][1]),2) + pow((xooz),2));
            uoo = uoo + (4 * 0.22454) * (pow((3.43/roo),12) - pow((3.43/roo),6));
        }
    }
    u = uco + uoo;
    return(u);
}


//--------------------------------------------------------------//

double x[1000][3];
double xn[3];
double temp[3];
double limit[3] = {6, 6, 20};
double c[1000][3];
long RAND = 3434534;
int nc, no, n;
double box = 24.6;

// x = coordinates of adsorbate
// temp = variable for storing 'new' coordinates of adsorbate
// temp  = variable for storing old coordinates of adsorbate if accepted
// limit = limit of dimentions where O atom can be inserted
// c = coordinates of absorbent


int main()
{

    cout << "Insert Number of Oxygen Molecules To Be Inserted: ";
    cin >> no;

    cout << "CHECK 1\n";


//------------------------- Reading File --------------------------



    cout << "Insert number of MC moves: ";
    int z;
    cin >> z;

    cout << z;
    cout << "\nCHECK 2\n";

//-------------------------Finding Number of Carbons-----------------

    std::ifstream cnt ("CNT.txt",std::ios::in);

    if (cnt.is_open())
        cout << "CHECK 3\n";

    int itr = 0;
    nc = 0;

    char atm[] = "ATOM";
    char atom[] = "ATOM";

    for (itr = 0; itr < 1000; itr++)
    {
        for (int p = 0; p < 4; p++)
        {
            cnt >> atom[p];
        }

        //      cout << '\t' << atom << '\t';                  //CHECK
        //              cout << atom;

        if (strcmp(atm,atom) == 0)
        {
            nc++;
            //    cout << atom;                  //CHECK
        }
        cnt.ignore(10000,'\n');
    }


    cnt.close();
    /*
        cout << nc;
        cout << itr;

        if (cnt.is_open());
        else
        cout << "CHECK 4\n";

    */

    cout << "CHECK 5\n";


//---------------Insertion Co-ordinates of Carbon In Array----------------
    cnt.open ("CNT.txt",std::ios::in);

    for (int q = 0; q < 4; q++)
        cnt.ignore(10000,'\n');


    for (int s = 0; s < nc; s++)
    {
        cnt.ignore(33,'-');
        cnt >> c[s][0] >> c[s][1] >> c[s][2];
        cnt.ignore(10000,'\n');
        cout << c[s][0] << c[s][1] << c[s][2];
    }


    cnt.close();

    cout << "CHECK 6\n";


// ----------------- Inserting Oxygen atoms ------------------------
    int a, b;

    for (a = 0; a < 3; a++)
    {
        for (b = 0; b < no; b++)
        {
//               cout << "\n" << x[a][b] << "\n";
            x[b][a] = (ran1(&RAND) - 0.5 ) * (limit[a]);
//               cout << "\n" << x[a][b] << "\n";
        }
    }

    cout << "CHECK 7\n";

    /* Repeat Translation Move 'z' Number of Times (User Specified) */


    int y;
    for (y = 0; y < z; y++)
    {

// --------------- Loop for performing 1 MC Move ------------------------------


        /* Randomly Select An Oxygen Molecule */
        // Oxygen Molecule = int (Rand(f) * No. of Oxygen) + 1

        n = (int (ran1(&RAND) * (no-1)));

        /* Randomly Generate New X,Y,Z Co-ordinates */
        //  x(new) = x(old) + ^x

        for (a = 0; a<3; a++)
        {
            xn[a] = x[n][a] + (ran1(&RAND) -0.5);
            temp[a] = x[n][a];
//        cout << n << '\t' << xn[a] << '\t' << temp[a] << '\t';
        }

        /* Call Old Energy, New Energy */

        double uo, un;

        uo = enr(no, nc, box, x[no][3], c[nc][3]);

        for (int g = 0; g < 3; g++)
            x[n][g] = xn[g];

        un = enr(no, nc, box, x[no][3], c[nc][3]);

        cout << '\n' << uo << '\t' << un << '\t';

        /* Find Out The Probability of Translation */

        double h, prob;

        h = 0.590195;
        prob = exp(-h*(un-uo));

//    cout << prob;

        /* Generate A Random Number (If Greater Than Probability Reject The Move Otherwise Accept It) */
        /* Rand(f);
           If Rand(f) < acc(o->n)
           x(o) = xn;
        */
        if (ran1(&RAND) > prob)
        {
            int i;
            for (i = 0; i<3; i++)
                x[n][i] = temp[i];
            cout << x[n][i];
        }
    }

    cout << "CHECK 8\n";

//----------- Writing Co-ordinates of Oxygen in File------------------

    std::ofstream cnt2 ("CNT2.txt",std::ios::out);


    int num = nc;
    for (n=0; n<no; n++)
        cnt2 << "ATOM    " << num++ << "  O   UNK     1      " << x[n][0] << "  " << x[n][1] << "  " << x[n][2] << "  1.00  0.00/n";

    cnt2 << "END";
    cnt2.close();
    //---------------------- CHECK --------------------------------

    cout << "END\n";

    cout << c[1][1] << "\n" << c[4][1] << "\n" << c[7][1] << "\n";    //CHECK Some Random Co-ordinates
    cout << no << "\n";                                               //CHECK No. of Oxygen
    cout << nc << "\n";                                               //CHECK No. of Carbon
    cout << num << "\n";                                              //CHECK No. of Carbon + Oxygen
    cout << enr(no, nc, box, x[no][3], c[nc][3]);                             //CHECK Final Energy


    return (0);
}
/* TODO (#1#): Not working  Check5-Check6 */
/* TODO (#2#): Not working  Energy Function (Used From Check7-Check8) */
/* TODO (#3#): Working  Till Check8 */

As for the actual problem, it is because the prototype for enr()

double enr(int, int, double, double, double);
[code]

does not match the function arguments in the actual function:

[code]
double enr(int no, int nc, double box, double x[][3], double c[][3])

You will need to change the prototype to something like this:

double enr(int, int, double, double[][3], double[][3]);

Also, you seem to be confused about the role of the function prototype; the purpose of having prototypes is to allow you to declare the function separately from the function itself, usually either at the beginning of the program or (preferably, given the size of this program) in a separate header file which you would include into the main program file. It makes no sense to have the function prototype directly above the function itself.

Edited 4 Years Ago by Schol-R-LEA: n/a

This question has already been answered. Start a new discussion instead.