error in macro expansion using sq()..

hey guys, i'm having a compiler error.. here's the error message:

note: in expansion of macro 'sq'
       totalDistance=sqrt(sq(vector[0]-feedback[0])+sq(vector[1]-feedback[1])sq(vector[2]-feedback[2]));
                                                                             ^
error: expression cannot be used as a function
 #define sq(x) ((x)*(x))
                       ^

the code in question is just a 3d distance formula:

void motion_planner(){                 
  if (moving== false) {
    if (execute==true) {
      totalDistance=sqrt(sq(vector[0]-feedback[0])+sq(vector[1]-feedback[1])sq(vector[2]-feedback[2]));
      for (int i=0; i<=2; i++) {
      velocitysetpoint[i]=((vector[i]-feedback[i])*(feedrate))/totalDistance;
      }
      execute=false;  
    }
    moving=true;
  }
}

any advice would be appreciated. thanks in advance!

Did you mean: "-feedback[1])**+**sq(vector[2]" :wink:

ugh, thanks lol :o :o :o

need more coffee this morning.

all good now!

At least you gave us all a good example why macro's should be something from the past... With a normal function the error description would be much clearer.

template <class T> T sqFunc(T a) {
  return a * a;
}

septillion:
With a normal function the error description would be much clearer.

Somewhat clearer, anyway:

/Users/john/Documents/Arduino/sketch_aug26a/sketch_aug26a.ino: In function 'void loop()':
sketch_aug26a:10:44: error: expected ')' before 'sqr'
   int totalDistance = sqrt(sqr(3) + sqr(4) sqr(5));
                                            ^
template <class T> inline T sqr(T a)
  {
    return a * a;
  }
void loop()
{
  int totalDistance = sqrt(sqr(3) + sqr(4) sqr(5));
}

I would say that's a lot better :slight_smile: At least it only points to where it's used.