简体   繁体   English

重载的函数调用不明确

[英]Overloaded function call double is Ambiguous

I am aware that similar questions have been asked, but I can't seem to find the solution. 我知道有人问过类似的问题,但我似乎找不到解决方法。 I've been using this code to return a function, but it doesn't seem to work: 我一直在使用此代码返回一个函数,但是它似乎不起作用:

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

I keep receiving this error message: 我不断收到此错误消息:

Arduino: 1.8.1 (Windows 7), Board: "Arduino/Genuino Uno" Arduino:1.8.1(Windows 7),开发板:“ Arduino / Genuino Uno”

C:\\Users\\family\\Documents\\Arduino\\GPStester\\GPStester.ino: In function 'double bearing(double, double, double, double)': C:\\ Users \\ family \\ Documents \\ Arduino \\ GPStester \\ GPStester.ino:在功能'double bearing(double,double,double,double,double)'中:

GPStester:90: error: call of overloaded 'print(double (&)(double, double, double, double))' is ambiguous GPStester:90:错误:重载的'print(double(&)(double,double,double,double,double))'的调用不明确

Serial.print(bearing); Serial.print(轴承);

note: no known conversion for argument 1 from 'double(double, double, double, double)' to 'long unsigned int' 注意:参数1从'double(double,double,double,double,double)'到'long unsigned int'的未知转换

exit status 1 call of overloaded 'print(double (&)(double, double, double, double))' is ambiguous ambiguous 退出状态1调用重载的'print(double(&)(double,double,double,double,double))'不明确

There are three issues with the code that stand out to me. 代码中有三个问题对我很突出。 First is that you overwrite the first two parameters a1 and a2 . 首先是您覆盖前两个参数a1a2 Whatever is passed to the function is being lost. 传递给函数的所有内容都会丢失。 Second, the Serial.print(bearing) call is unreachable code, and would never be called even if it were not throwing a compiler error. 其次, Serial.print(bearing)调用是无法访问的代码,即使它没有Serial.print(bearing)编译器错误,也永远不会被调用。

A quick internet search of "Arduino Serial print" found descriptions of a method that will take integers and floats and send an ASCII representation of the values over a serial connection. 在互联网上快速搜索“ Arduino串行打印”后,找到了一种方法的说明,该方法将采用整数和浮点数,并通过串行连接发送值的ASCII表示。 I am guessing this is the function you are using. 我猜这是您正在使用的功能。

However, you function call is trying to pass a pointer to a function to the Serial.print() call instead of a float or integer value. 但是,您的函数调用试图将指向函数的指针传递给Serial.print()调用,而不是浮点或整数值。 If your desire is to print the results of the call to bearing out to the serial link, you should be doing so with the return value of the function, not in the function itself with a pointer to the function. 如果您希望将调用结果打印出来以支持串行链接,则应该使用函数的返回值来执行此操作,而不是使用指向函数的指针在函数本身中执行此操作。

Somewhere in your code you are going to call bearing . 在代码中的某个地方,您将调用bearing I suspect you want it to look like this: 我怀疑您希望它看起来像这样:

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

This will make the call to bearing with the desired parameters, and then send the result to the serial port. 这将使呼叫bearing与所需的参数,然后将结果发送到串行端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM