无法通过参考

时间:2017-01-31 18:22:36

标签: c++ pass-by-reference pass-by-value

在我的代码中,我一直收到一个错误,即主要功能中的“n”未声明,即使我在“double mols”声​​明它

#include <iostream>

 using namespace std;
 const double idealGas = 8.3144598;


 double mols(double mass, double molarMass);
 double pressure(double v, double n, double t);

 int main() {

     cout << "the mols are " << mols(5.0,20.0) <<  "\n" << "the pressure is" << pressure(3.0,n,293) << endl;

     return 0;
 }

 double mols(double mass, double molarMass){

     double n = mass/molarMass;
     return n;
 }

 double pressure(double v, double n, double t){

     double p = (n*idealGas*t)/v;
     return p;
 }

1 个答案:

答案 0 :(得分:1)

n不在主要功能的范围内

cout << "the mols are " << mols(5.0,20.0) <<  "\n" << "the pressure is" << pressure(3.0,n,293) << endl;

固定:

#include <iostream>

 using namespace std;
 const double idealGas = 8.3144598;


 double mols(double mass, double molarMass);
 double pressure(double v, double n, double t);

 int main() {

     double n = mols(5.0, 20.0);

     cout << "the mols are " << n <<  "\n" << "the pressure is" << pressure(3.0,n,293) << endl;

     return 0;
 }

 double mols(double mass, double molarMass){

     double n = mass/molarMass;
     return n;
 }

 double pressure(double v, double n, double t){

     double p = (n*idealGas*t)/v;
     return p;
 }