【问题标题】:none of the following functions can be called with the arguments supplied kotlin error不能使用提供的参数调用以下函数 kotlin 错误
【发布时间】:2018-09-04 07:06:18
【问题描述】:

我正在尝试一些 kotlin 的基础知识 ->

程序:

fun createDate(day: Int, month: Int, year: Int, hour: Int = 0, minute: Int = 0, second: Int = 0) {
print("TEST", "$day-$month-$year $hour:$minute:$second")
}
createDate(1,7,1997)

错误:

error: none of the following functions can be called with the arguments supplied: 
@InlineOnly public inline fun print(message: Any?): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Boolean): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Byte): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Char): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: CharArray): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Double): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Float): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Int): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Long): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Short): Unit defined in kotlin.io
print("TEST", "$day-$month-$year $hour:$minute:$second")

知道我做错了什么,我一直在关注这个 -> https://www.toptal.com/software/kotlin-android-language

【问题讨论】:

  • print 只接受一个参数,你要传递两个参数。
  • 消息比较清楚......只存在具有1个参数的方法,但您传递了2个(第一个。“TEST”,第二个:“$day-...$second”)。您可能想使用类似的东西:“TEST $day-...$second”。请注意,我不知道那篇文章的作者使用了哪个print-function……可能是自建的……
  • 天哪...那篇文章已经有 2 年多的历史了,并且使用 Kotlin 版本 1.0.1...您可能想要使用最近编写的教程或者如果您喜欢这种风格只是带着一粒盐......

标签: kotlin


【解决方案1】:

应该是这样的,因为kotlin默认的打印函数只有一个参数

fun createDate(day: Int, month: Int, year: Int, hour: Int = 0, minute: Int = 0, second: Int = 0) {
    print("$day-$month-$year $hour:$minute:$second")
}

因为kotlin默认的打印功能是这样的

/** Prints the given message and newline to the standard output stream. */
public expect fun println(message: Any?)

 /** Prints the given message to the standard output stream. */
public expect fun print(message: Any?)

所以你不能在打印函数中发送双参数。所以使用

print("$day-$month-$year $hour:$minute:$second")

而不是

print("TEST", "$day-$month-$year $hour:$minute:$second")

【讨论】:

  • 主要问题原来是打印函数中的参数数量。谢谢
【解决方案2】:

在线出错

createDate(1,7,1997)

因为您调用了 createDate() 函数,但 参数 不匹配

让我们重写这个函数:

createDate(1,7,1997,5,24,15)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多