c++ - 从另一个函数引用函数

标签 c++ function

我忘记了如何在 C++ 中将另一个函数引用到一个函数中? 在 Python 中,它被声明为一个类,以便我可以使用它。

double footInches(double foot)
{
 double inches = (1.0/12.00) * foot;
 return inches;
}

double inchMeter(double inch)
{
 double meter = 39.37 * (footInches(inch));
 return meter;
}

我想在 inchMeter 中引用 footInches。

已编辑

这是主程序

int main()
{
 double feet, inch, meter, x = 0;

 cout << "Enter distance in feet to convert it to meter: " << endl;
 cin >> x;

 cout << inchMeter(x);

 return 0;

我认为最后一行不正确。我想先获取以 footInches 为单位的 x,然后转到 inchMeter,然后从 inchMeter 返回答案。

最佳答案

引用是指调用吗?
您在示例中正确调用了该函数,但不需要周围的括号。

就这么简单:

double inchMeter(double inch)
{
 double meter = 39.3700787 * footInches(inch);
 return meter;
}

如果您的函数存在于不同的 .cpp 文件中,或者您需要引用稍后定义的函数,您可以使用前向声明。

例子:

a.cpp:

double footInches(double foot)
{
 double inches = foot * 12.0;
 return inches;
}

b.cpp:

double footInches(double foot); //This is a forward declaration

double inchMeter(double inch)
{
 double meter = 39.3700787 * footInches(inch);
 return meter;
}

关于c++ - 从另一个函数引用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2478566/

上一篇:C++ cin一直在跳

下一篇:C++ 输入流

相关文章:

python - 优化二叉树函数,霍夫曼树

Javascript - 原型(prototype)设计的值(value)是什么?

Ruby:传递给函数的变量被更改

c++ - typedef 和在同一范围内使用相同名称的声明

c++ - 使用 C++ Winsock API 上传 PNG 文件(HTTP POST)

c++ - N 位环绕的整数减法

c++ - 从 COBOL 生成的 .DAT 文件进行数据转换

c++ - boost::program_options 附加具有相似名称的 vector 选项

将一阶传递函数转换为 C 代码

javascript - 如何调用 Meteor.methods 中的函数并返回值