English 中文(简体)
过错:不止一次超负荷功能“泥土”与论据清单相吻合。 什么?
原标题:Error: More than one instance of overloaded function "sqrt" matches the argument list. What that?
  • 时间:2011-10-31 22:36:38
  •  标签:
  • c
  • math

在我的C级的家务劳动中,我们必须写出一项方案,以检查如果愤怒是主人的话。 我在泥土功能上错了。 我的教授告诉我,南德必须是一种愤怒,我们必须利用泥土功能。 我认为,问题在于, s())功能可能被用于愤怒,但我的教授告诉我,它能够,而且我正在从别的东西中犯错误。 你们是否看待这一问题?

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.

错误是“错误:不止一次超负荷功能“侵权”与争论清单相匹配。

最佳回答

C语言没有“超负荷”。 我很幸运的是,你正在把你的法典编成C++,而不是C。 如果您重新使用海合会,则汇编成<条码>gcc,而不是<条码>g++/代码>。 如果你使用视频演播室,该项目的特性可以选择:

  1. right click on your project
  2. click properties
  3. click on C/C++
  4. click on advanced
  5. set the property "compile as" to C Code (/TC)

在任何一种情况下,都以<代码>c的延伸(downcase c)命名您的档案。

事实上,在C中,只有一个“<条码> qrt,被定义为:

double sqrt(double);

ger化物转化成两倍。

问题回答

您不想在座右边打上return 1,而是在休息时间之后。 教授是正确的,sqrt(num)将工作——num将自动升至double——C有关于职能电话类型变化的规则等。 您确实需要列入<代码>math.h。 如果你没有在你方案的其他部分这样做的话,你会发现什么错误?

你的逻辑是错误的:

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

如果您认为,如果数字不二分之八,那么你的职责就是立即返回<代码>1。

页: 1 请注意<代码>return基本上停止了您的职能并中断了。

相反,当编号为复合和返回时,只试图打破 > <>true 只限<>。 当你确信这是首要的。

仅作为附带说明,您的职能将登记标本(即9,25,49)为主要内容,因为您正在使用<for loop>上签字。

Change it to a <= sign, as that will account for the square root being the only divisor of the number.

页: 1

而且,根据你的职能,1、2和3项不是主要内容。 想确定这一点。 我提供了更多的帮助,但是,这好像是家庭工作,我不只给你回答(一些大学认为是che的)。

To answer your actual question: sqrt() does not have an overload for int, but it has overloads for both double and float, and so the compiler cannot guess which one to use.

The problem is that the sqrt function does not take an integer as a argument. You will want to do

sqrt((double)n) or sqrt((float)n) to fix it.

如果列入<代码>#include <math.h>, 您可确定以下错误:for (int i = 2; i <= sqrt(num); i++),将sqrt(num)改为简单申报的变量(如n, x, i.),因为sqrt(num)已在中使用的。

<代码>sqrt() 的定义为double sqrt(double),因此,你的再投入应增加一倍或浮动。

将<代码>integer变量改为float ,但未作改动。 缩略语

例如,在你重新编号时,请填写i<=sqrt(float(num));





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...