我忘记了如何在 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/