【问题标题】:Overloaded function call double is Ambiguous重载的函数调用 double 是不明确的
【发布时间】:2017-06-01 00:47:43
【问题描述】:

我知道有人问过类似的问题,但我似乎找不到解决方案。我一直在使用这段代码返回一个函数,但它似乎不起作用:

#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),板:“Arduino/Genuino Uno”

C:\Users\family\Documents\Arduino\GPStester\GPStester.ino:在函数中 '双轴承(双,双,双,双)':

GPStester:90: 错误: 调用重载 'print(double (&)(double, double, double, double))' 模棱两可

Serial.print(轴承);

注意:没有已知的参数 1 从 'double(double, double, double, double)' 到 'long unsigned int' 的转换

退出状态 1 重载 'print(double (&)(double, double, double, double))' 的调用是模棱两可的模棱两可

【问题讨论】:

  • Serial.print(bearing); 似乎是无法访问的代码。
  • 我没有编程 arduino 但错误消息似乎表明Serial.print 预期的参数是long unsigned int。您正在将一个指向函数的指针传递给它。
  • 在注释中说“点 B(b1,b2)”,然后在参数中说“deg”和“degy”,非常令人困惑。这些参数是否是“B”点?这些名称看起来像是以度数表示角度。
  • 注意:在 Arduino 上,floatdouble 都是 4 个字节。 double 不是双精度。

标签: c++ arduino double overloading arduino-ide


【解决方案1】:

对我来说,代码存在三个问题。首先是覆盖前两个参数a1a2。传递给函数的任何内容都将丢失。其次,Serial.print(bearing) 调用是无法访问的代码,即使它没有引发编译器错误,也永远不会被调用。

在互联网上快速搜索“Arduino Serial print”发现了一种方法的描述,该方法将采用整数和浮点数并通过串行连接发送值的 ASCII 表示。我猜这是你正在使用的功能。

但是,您的函数调用试图将指向函数的指针传递给Serial.print() 调用,而不是浮点或整数值。如果您希望将调用结果打印到串行链接,您应该使用函数的返回值,而不是在函数本身中使用指向函数的指针。

您将在代码中的某处调用bearing。我怀疑你希望它看起来像这样:

Serial.print(bearing(a1,a2,deg,degy));

这将使用所需参数调用bearing,然后将结果发送到串行端口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多