简体   繁体   English

错误:重载函数“ sqrt”的多个实例与参数列表匹配。 那是什么

[英]Error: More than one instance of overloaded function “sqrt” matches the argument list. What that?

In my homework for my C class, we have to write a program that checks if an integer is prime. 在我的C类作业中,我们必须编写一个程序检查整数是否为质数。 I am getting an error at the sqrt() function. 我在sqrt()函数中遇到错误。 My professor told me that num must be an integer and we must use the sqrt() function. 我的教授告诉我,num必须为整数,并且必须使用sqrt()函数。 I thought the problem was that the sqrt() function can't be used on an integer but my professor told me that it can, and that I was getting an error from something else. 我以为问题是sqrt()函数不能在整数上使用,但是我的教授告诉我可以使用,并且我从其他错误中获取了错误。 Do you guys see the problem? 你们看到问题了吗?

int primality(int num)
{

int isprime;

    /*check if num is prime*/
    for (int i = 2; i <= sqrt(num); i++)
    {
        if (num % i == 0)
            isprime = 0; /*is not prime*/
        else 
            isprime = 1; /*is prime*/
    }

    if (isprime == 0)
        return 0;
    elseif (isprime == 1)
        return 1;
}

EDIT: Yes I am using math.h and compiling as C code. 编辑:是的,我正在使用math.h并编译为C代码。

The error msg is "Error: More than one instance of overloaded function "sqrt" matches the argument list. 错误消息是“错误:重载函数“ sqrt”的多个实例与参数列表匹配。

The C language does not have "overloads". C语言没有“重载”。 My bet is that you are compiling your code as C++, not C. If you're using GCC, compile with gcc , not g++ . 我敢打赌,您是将代码编译为C ++而不是C。如果使用的是GCC,请使用gcc而不是g++ If you are using Visual Studio, there is an option in the properties of the project: 如果使用的是Visual Studio,则项目属性中有一个选项:

  1. right click on your project 右键点击您的项目
  2. click properties 点击属性
  3. click on C/C++ 点击C / C ++
  4. click on advanced 点击高级
  5. set the property "compile as" to C Code (/TC) 将属性“编译为”设置为C代码(/ TC)

In either case, name your file with a .c extension ( lowercase 'c'). 无论哪种情况,都以.c扩展名( 小写的 'c')命名文件。

Indeed, in C there is only one sqrt , defined as 确实,在C中只有一个sqrt ,定义为

double sqrt(double);

and integers convert to double. 和整数转换为双精度。

You don't want to return 1 inside your loop - it needs to be after the loop completes. 您不想在循环内return 1它需要在循环完成之后返回。 Your professor is right that sqrt(num) will work - num will be promoted to double automatically - C has rules for type changes in function calls, etc. You do need to include math.h if you haven't done that elsewhere in your program - what error are you getting? 您的教授是对的, sqrt(num)将起作用num将自动提升为double -C在函数调用等中具有用于类型更改的规则。如果您未在其他地方进行操作,则需要包括math.h程序-您遇到什么错误?

Your logic is wrong right here: 您的逻辑在这里是错误的:

    if (num % i == 0)
        return 0; /*is not prime*/
    else 
        return 1; /*is prime*/

If you think about it, if the number is not divisible by two, your function immediately returns 1 . 如果考虑一下,如果数字不能被2整除,则函数将立即返回1

You are return ing too early. return太早了。 Remember that a return essentially stops your function and breaks out of it. 请记住, return实际上会停止您的功能并中断该功能。

Instead, try to break out only when the number is composite and return True only when you are sure that it is prime. 相反,请尝试在数字为复合数时才爆发,并在确定为质数时才返回True

Just as a side note, your function will register squares of primes (ie 9 , 25 , 49 ) as prime because you are using the < sign in your for loop. 正如一个侧面说明,你的函数将注册素数的平方(即92549 )为总理,因为你使用的是<符号在你for循环。

Change it to a <= sign, as that will account for the square root being the only divisor of the number. 将其更改为<=符号,因为这将说明平方根是数字的唯一除数。

The problem is that the sqrt function does not take an integer as a argument. 问题在于sqrt函数不采用整数作为参数。 You will want to do 你会想做的

sqrt((double)n) or sqrt((float)n) to fix it. sqrt((double)n)或sqrt((float)n)进行修复。

如果包含#include <math.h> ,则可以通过将sqrt(num)更改为简单的声明变量(例如n, x, i... sqrt(num)来修正以下错误: for (int i = 2; i <= sqrt(num); i++) n, x, i... ),因为sqrt(num)已在math.h库中使用。

the function of sqrt() define as double sqrt(double) so you're input should be double or float. sqrt()的函数定义为double sqrt(double)因此您输入的内容应为double或float。

To turning an integer variable to float without change the type use float( integer_Variable ) 要将integer变量转换为float而不更改类型,请使用float( integer_Variable )

For example in you're code write i<=sqrt(float(num)); 例如,在您的代码中,写i<=sqrt(float(num));

Did you #include <math.h> ? 您是否#include <math.h>

Also, according to your function 1, 2, and 3 come back as not prime... Might want to fix that. 另外,根据您的功能1、2和3可能不是素数。可能要解决这个问题。 I'd offer more help but it sounds like this is for homework and I'd rather not just give you the answer (some universities consider it cheating). 我会提供更多帮助,但这听起来像是在做作业,我宁愿不只是给您答案(有些大学认为这是作弊的)。

要回答您的实际问题:sqrt()的int没有重载,但double和float都有重载,因此编译器无法猜测要使用哪个重载。

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

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