gpt4 book ai didi

c++ - 错误: expression cannot be used as a function?

转载 作者:行者123 更新时间:2023-12-02 10:49:06 29 4
gpt4 key购买 nike

我在程序中创建了一个快速方法,使用距离公式来计算两点之间的距离,这是代码:

#include <iostream>
#include <cmath>

using namespace std;

int distanceFormula(int x1, int y1, int x2, int y2) {
double d = sqrt((x1-x2)^2(y1-y2)^2);
return d;
}
它在我声明 d变量的行上给了我一个编译器错误,说 error: expression cannot be used as a function
这是什么意思?我在做什么错呢?

最佳答案

请注意,(x1-x2)^2此处的指数不会为2。
参见http://www.cplusplus.com/reference/cmath/pow/

其次,您可能忘记了表达式中的+:

int distanceFormula(int x1, int y1, int x2, int y2) {
double d = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
return d;
}

关于c++ - 错误: expression cannot be used as a function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617609/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com