在extern'之前的预期初始化程序当使用math.h时

时间:2012-04-14 23:40:14

标签: c++ math.h

错误指向math.h文件的第36行,我没有搞砸过它。 SRK.cpp是唯一需要标头的文件,但它不会,因此将它包含在头文件中似乎是合乎逻辑的。显然有些东西搞砸了(我个人认为那里有一个拼写错误或其他简单的错误,但是由于错误指向我包含在C ++中的头文件,我不知道在哪里看)。

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);
}

2 个答案:

答案 0 :(得分:5)

此:

double SRK(double Tr, double Pr, double acc)
#include <math.h>
{

无效。您不能在函数签名及其正文之间放置#include系统头文件。

将其更改为:

#include <math.h>
double SRK(double Tr, double Pr, double acc)
{

一般情况下,在您的任何代码之前,#include行应放在.cpp文件的顶部。

答案 1 :(得分:2)

SRK.cpp中,您必须将#include <math.h>移动到文件的开头。此外,在header.h中,您严格不需要math.h,因此您不必包含它。