Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: expression cannot be used as a function?

I created a quick method in my program to compute the distance between two points using the distance formula, here's the code:

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

it gives me a compiler error on the line where I declare the d variable saying that

error: expression cannot be used as a function.

What does this mean? And what am I doing wrong?

like image 350
LoreleiRS Avatar asked Jan 24 '26 00:01

LoreleiRS


1 Answers

Be careful, (x1-x2)^2 will not do an exponent of 2 here. See http://www.cplusplus.com/reference/cmath/pow/.

Second, you probably forgot a + in your expression:

int distanceFormula(int x1, int y1, int x2, int y2) {
    double d = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
    return d;
}
like image 169
Mr_Pouet Avatar answered Jan 25 '26 15:01

Mr_Pouet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!