problem with static variable passing to objects

Hi, I've got a static variable within a .h file and it seems to be initialised fine, but when i try to adjust it its coming out with a -1.#IND runtime error.

The static variable in question is the sumPhotons variable.

the relevant lines in the .cpp file that call the class are:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>
#include "phot.h"

using namespace std;

double phot::sumPhotons = 393400000;
const int phot::totalPhotons = 409460000;
const double phot::constant = 131900000/2;

int main(){

const int MAX = 128;
int index=0,i=0,j=0,n,q;
double m;
phot light[MAX];
double numPhotons, lTemp, lpercent;


while(index<MAX && index>=0){

for(int k=0; k<MAX; k++){
light[index].lAssignEnergy(index, k);
light[index].lAssignPhotonPercentage(k);

}
index++;
}

system("pause");
return 0;

}

And the .h file is:


#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <math.h>
#ifndef PHOT_H
#define PHOT_H

using namespace std;

class phot{

private:
double lEnergy[128];
double lTemp[128];
double percentagePhotons[128];
static const int totalPhotons;
static double sumPhotons;
static const double kT=0.00023481;
double numPhotons;
static const double constant;//this constant is the 1/(hbar^3*pi^2*c^3). C didnt like the size of it in the numphotons line though so I've adjusted for it later

double dNe(double E1, double E2){
double E1exp = exp(E1/kT);
double E2exp = exp(E2/kT);
double numPhotons =constant*((E1*E1)/(E1exp-1)+(E2*E2)/(E2exp-1))*(E2-E1);
numPhotons*=100000000;//adjustment to the constant
numPhotons*=1000;
return numPhotons;
}


public:
void lAssignEnergy(int i, int j){
lTemp[j] = 17.5 + (0.256*i) + 0.002*j;
lEnergy[j] = lTemp[j]*0.00008617;
}
void lAssignPhotonPercentage(int j){
numPhotons = dNe((lTemp[j-1]*0.00008617), (lTemp[j]*0.00008617));
sumPhotons+=numPhotons;
percentagePhotons[j]=(((double)totalPhotons)-sumPhotons)*100/((double)totalPhotons);}

double getnumPhotons(){
return numPhotons;
}
double getlTemp(int j){
return lTemp[j];
}
double getPhotonPercentage(int j) {

return percentagePhotons[j];
}

};

#endif

Also, if anyone has some insight, the percentage values im getting are about 2% less than they should be? That would be a serious help but then it wouldn't be a purely programming error so its not really what the forum is here for i suppose!
static const double kT=0.00023481;

You can't initialize static const doubles like this. Initialize it like the other static variables.

numPhotons = dNe((lTemp[j-1]*0.00008617), (lTemp[j]*0.00008617));

The j-1 here is out of bounds when j is zero.
Topic archived. No new replies allowed.