I have 2 files: a .h file and a .cpp file. I am new to c++ but need to know how to execute these files. In other words, how do I write a 'main' so I can actually get an output to these 2 files. I am using Microsoft Visual studio 6.0.

//-------------------------------------------------------------------------
#ifndef ErfnH
#define ErfH
//-------------------------------------------------------------------------

class Erfn
{
public:
    class ErfnRange{};
    double *ErfnTable;
    double operator()(double x);
    Erfn(void);
    ~Erfn(void);
};

extern Erfn erfn;

#endif

//Erfn.cpp

//-------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <math.h>
#include "Erfn.h"

//-------------------------------------------------------------------------
#pragma package(smart_init)
#define ERFNTABLESIZE 2501
#define pi 3.1415926536
Erfn erfn;                         

double MaxX;

Erfn::Erfn(void)
{
    MaxX=200.0;
    ErfnTable=new double[ERFNTABLESIZE];
    double rootpi=sqrt(pi);
    int i;
    double t,dt;
    for(i=0;i<ERFNTABLESIZE;i++)
    {
        double val=0;
        int j;
        dt=1.0/(double)(ERFNTABLESIZE-1)*MaxX;
        t=0.5/(double)(ERFNTABLESIZE-1)*MaxX;
        for(j=0;j<i;j++)
        {
            val+=2.0/rootpi*exp(-t*t)*dt;
            t+=dt;
        }
        if(val>1)
            val=1;
        ErfnTable[i]=val;
    }
}
Erfn::~Erfn(void)
{
    delete [] ErfnTable;
}

double Erfn::operator()(double x)
{           
    double val;
    int index;
    if(x<0)
    {
        throw ErfnRange();
    }
    index=x*(double)(ERFNTABLESIZE-1)/MaxX;
    if(index>=ERFNTABLESIZE-1)
    {
        return 1-exp(-x*x)/(x*sqrt(pi))*(1-1/(2*x*x));
    }
    double interp=(x*(double)(ERFNTABLESIZE-1)/MaxX-index);
    val=ErfnTable[index]*(1-interp)+ErfnTable[index+1]*interp;
    return val;
}

Edited 3 Years Ago by happygeek: fixed formatting

I didn't look thru your code but I can give you some basic ideas here.
Most of time, you have your main() in a seprated file, and just include the .h you are gonna use in the header, usually you include <iostream> as well. Then often you create an object of that class and with that object you can call functions to manipulate it.

The codes you have now actually are not good, they are missing function descriptions and pre/post conditions.

I know I need a main().
The question was how to write a main() to call these functions/classes
so I can get the output.

I didn't look thru your code but I can give you some basic ideas here.
Most of time, you have your main() in a seprated file, and just include the .h you are gonna use in the header, usually you include <iostream> as well. Then often you create an object of that class and with that object you can call functions to manipulate it.

The codes you have now actually are not good, they are missing function descriptions and pre/post conditions.

#include <iostream>
 #include "Erfn.h"
 
 int main()
 {
    Erfn var;
    for ( double i = 0; i < 4.25; i += 0.5 )
    {
 	  std::cout << "Erfn(" << i << ") = " << var(i) << std::endl;
    }
    return 0;
 }
 
 /* my output
 Erfn(0) = 0
 Erfn(0.5) = 0.5202
 Erfn(1) = 0.842258
 Erfn(1.5) = 0.96598
 Erfn(2) = 0.995344
 Erfn(2.5) = 0.99959
 Erfn(3) = 0.999977
 Erfn(3.5) = 0.999999
 Erfn(4) = 1
 */
This article has been dead for over six months. Start a new discussion instead.