Binary operator '/' cannot be applied to two (Int) operands [duplicate]

给你一囗甜甜゛ 提交于 2019-12-10 17:22:49

问题


I am getting a Binary operator '/' cannot be applied to two (Int) operands error when I put the following code in a Swift playground in Xcode.

func sumOf(numbers: Int...) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
sumOf()
sumOf(42, 597, 12)

The above was a function calculating the total sum of any numbers. Below is a function calculating the average of the numbers. The function is calling the sumOf() function from within itself.

func avg(numbers: Int...) -> Float {
    var avg:Float = ( sumOf(numbers) ) / ( numbers.count ) //Binary operator '/' cannot be applied to two (Int) operands
    return avg
}

avg(1, 2, 3);

Note: I have looked everywhere in stack exchange for the answer, but the questions all are different from mine because mine is involving two Ints, the same type and not different two different types.

I would like it if someone could help me to solve the problem which I have.


回答1:


Despite the error message it seems that you cannot forward the sequence (...) operator. A single call of sumOf(numbers) within the agv() function gives an error cannot invoke sumOf with an argument of type ((Int))




回答2:


The error is telling you what to do. If you refer to https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_operators.html

/  Division.
A binary arithmetic operator that divides the number to its left by the number to its right.
Class of operands: integer, real
Class of result: real    

The second argument has to be real. Convert it like so. I don't use xcode, but I think my syntax is correct.

var avg:Float = ( sumOf(numbers) ) / Float( numbers.count ) 


来源:https://stackoverflow.com/questions/31132491/binary-operator-cannot-be-applied-to-two-int-operands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!