Soroush Motesharei
Soroush Motesharei

Reputation: 13

I keep getting the error "cannot convert 'float*' to 'float' in return"

I am new to c++ and I'm using the Arduino platform. I was writing a program for my project and at one point I need to convert cartesian coordinate system to cylindrical coordinate system. The program takes in a float array of size 3 and does some stuff to it and returns a new float array of size 3 with the coordinates in the other system. I keep getting the error "exit status 1, cannot convert 'float*' to 'float' in return" and I have absolutely no idea what's wrong with my code or how to fix it. can somebody please help me understand whats going on?

float CartesianToCylindrical (float pos[]){          //pos is in the form of [x,y,z]//
 float cylpos[3];
 cylpos[0] = sqrt((pos[0] ^ 2) + (pos[1] ^ 2));
 cylpos[1] = atan(pos[1] / pos[0]);
 cylpos[2] = pos[2];
 return cylpos;                                      //return in the form of [r,theta,z]//

Upvotes: 0

Views: 614

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73181

Unfortunately, C-style arrays aren't first-class objects in C++, which means you can't easily return them from a function the same way you would other object types. There are ways around that limitation, but they are awkward; the best approach for C++ is to define an object-type instead, like this:

#include <math.h>
#include <array>
#include <iostream>

// Let's define "Point3D" to be an array of 3 floating-point values
typedef std::array<float, 3> Point3D;

Point3D CartesianToCylindrical (const Point3D & pos)
{
   //pos is in the form of [x,y,z]//
   Point3D cylpos;
   cylpos[0] = sqrt((pos[0] * pos[0]) + (pos[1] * pos[1]));
   cylpos[1] = atan(pos[1] / pos[0]);
   cylpos[2] = pos[2];
   return cylpos;
}

int main(int, char **)
{
   const Point3D p = {1,2,3};
   const Point3D cp = CartesianToCylindrical(p);
   std::cout << "(x,y,z) = " << cp[0] << ", " << cp[1] << ", " << cp[2] << std::endl;
}

.... that way you can pass and return your point-values in a natural manner.

Upvotes: 3

Related Questions