expected initializer before 'extern' when using math.h
Question
The error points me to line 36 of the math.h file, which I haven't messed with. SRK.cpp is the only file that needs the header, but it won't be, so it seemed logical to include it in the header file. Clearly something is messed up (personally I expect there's a typo or other simple mistake in there, but since the error is pointing me to the header file included with C++ I don't know where to look).
header.h
#ifndef HEADER_H
#define HEADER_H
#include <math.h>
double reducedP(double P, double Pc);
double reducedT(double T, double Tc);
double SRK(double Tr, double Pr, double acc);
#endif
main.cpp
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
double T = 0;
double Tc = 0;
double Tr = 0;
double P = 0;
double Pc = 0;
double Pr = 0;
double acc = 0;
double Z = 0;
cout << "Enter Temperature: ";
cin >> T;
cout << "\n";
cout << "Enter Pressure: ";
cin >> P;
cout << "\n";
cout << "Enter Critical Temperature: ";
cin >> Tc;
cout << "\n";
cout << "Enter Critical Pressure: ";
cin >> Pc;
cout << "\n";
Tr = reducedT(T,Tc);
Pr = reducedP(P,Pc);
cout << "Reduced T,P\t\t\t" << Tr << "\t\t" << Pr <<"\n";
cout << "Enter accentric factor: ";
cin >> acc;
cout << "\n";
Z = SRK(Tr, Pr, acc);
cout << "Z is " << Z << "\n";
return 0;
}
SRK.cpp
double SRK(double Tr, double Pr, double acc)
#include <math.h>
{
double alpha;
double phi = 1;
double epsilon = 0;
double omega = 0.08664;
double psi = 0.42748;
double Zc = 1/3;
double a = (1+(0.480 + 1.574*acc - .176*acc*acc)*(1-sqrt(Tr)));
alpha = pow(a,2);
cout << "Alpha is " << alpha << "\n";
double beta = omega*(Pr/Tr);
cout << "beta is " << beta << "\n";
double q = (psi*alpha)/(omega*Tr);
cout << "q is " << q << "\n";
double Z = 0;
double test = 0;
double Z_init = 1;
while(fabs(Z_init-test)>.00001)
{
Z = 1 + beta - (q*beta)*((Z_init - beta)/((Z_init)*(Z_init+beta)));
cout << "\n" << Z_init << "\n";
test = Z_init;
Z_init = Z;
}
return (Z);
}
Solution
This:
double SRK(double Tr, double Pr, double acc)
#include <math.h>
{
is invalid. You can't put a #include
of a system header file between a function signature and its body.
Change it to:
#include <math.h>
double SRK(double Tr, double Pr, double acc)
{
In general, your #include
lines should go at the top of your .cpp
file, before any of your code.
OTHER TIPS
In SRK.cpp
you have to move the #include <math.h>
to the beginning of the file. Also, in header.h
you strictly don't need math.h
, so you don't have to include it.