我知道有人问过类似的问题,但我似乎找不到解决方案。我一直在使用这段代码来返回一个函数,但它似乎不起作用:
#include <math.h>
// Computes the bearing in degrees from the point A(a1,a2) to
// the point B(b1,b2). Note that A and B are given in terms of
// screen coordinates
double bearing(double a1, double a2, double deg, double degy) {
a1 = 37.40733;
a2 = -121.84855;
static const double TWOPI = 6.2831853071795865;
static const double RAD2DEG = 57.2957795130823209;
// if (a1 = b1 and a2 = b2) throw an error
double theta = atan2(deg - a1, degy + a2);
if (theta < 0.0)
theta += TWOPI;
return RAD2DEG * theta;
Serial.print(bearing);
}
我不断收到此错误消息:
Arduino: 1.8.1 (Windows 7), Board: "Arduino/Genuino Uno"
C:\Users\family\Documents\Arduino\GPStester\GPStester.ino: In function 'double bearing(double, double, double, double)':
GPStester:90: error: call of overloaded 'print(double (&)(double, double, double, double))' is ambiguous
Serial.print(bearing);
note: no known conversion for argument 1 from 'double(double, double, double, double)' to 'long unsigned int'
exit status 1 call of overloaded 'print(double (&)(double, double, double, double))' is ambiguous ambiguous
最佳答案
我对代码有三个突出的问题。首先是覆盖前两个参数 a1
和 a2
。传递给函数的任何内容都将丢失。其次,Serial.print(bearing)
调用是无法访问的代码,即使它没有引发编译器错误也永远不会被调用。
在互联网上快速搜索“Arduino Serial print”找到了一种方法的描述,该方法将采用整数和 float 并通过串行连接发送值的 ASCII 表示。我猜这就是您正在使用的功能。
但是,您的函数调用试图将指向函数的指针传递给 Serial.print()
调用,而不是 float 或整数值。如果您希望将对 bearing 的调用结果打印到串行链接,您应该使用函数的返回值来执行此操作,而不是在函数本身中使用指向函数的指针。
您将在代码中的某处调用bearing
。我怀疑您希望它看起来像这样:
Serial.print(bearing(a1,a2,deg,degy));
这将使用所需的参数调用bearing
,然后将结果发送到串口。
关于c++ - 重载函数调用 double 是不明确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41688952/