Scala错误导入

时间:2017-01-23 02:59:50

标签: scala

我正在学习Scala,我试图从不同课程的2个文章中导入,但是,我收到错误,我不知道为什么。

这个类在Summer.scala中,我尝试从类ChecksumAccumulator导入单例对象。

import ChecksumAccumulator.calculate

object Summer{
  def main(args: Array[String]): Unit= {
    for (arg <- args)
      println(arg + " : " +  calculate(arg))
  }
}

Class ChecksumAccumulator,使用单例对象ChecksumAccumulator。

object ChecksumAccumulator {

/*
 When a singleton object shares the same name
 with a class, it is called that class's companion object. 
*/

  private val cache = mutable.Map.empty[String, Int]

  def calculate(s: String): Int =
    if (cache.contains(s))
      cache(s)
    else {
      val acc = new ChecksumAccumulator

      //The object with the same name of the class can acces to the private members.
      //println(acc.sum)
      for (c <- s)
        acc.add(c.toByte)
      val cs = acc.checksum()
      cache += (s -> cs)
      cs
    }
    def showMap() : Unit = {
      for(base:String <- cache.keys)
        println(cache(base))
    }
//def showMap(): String = { var cadena = cache("Every value is an object.") +  " "+ cache("Every value is an object. per second time!!");  cadena  }
}

这个脚本抛出了这个错误

  

scala Summer.scala

     

Summer.scala:8:错误:未找到:对象ChecksumAccumulator导入   ChecksumAccumulator.calculate ^ Summer.scala:14:错误:未找到:   value计算println(arg +&#34;:&#34; + calculate(arg))^两个错误   结果

1 个答案:

答案 0 :(得分:0)

好像是duplicated question,但我还不能将问题标记为重复,也不能对其进行评论。

如果那是你的全部代码,那就意味着你错过了:

class ChecksumAccumulator { ... }

因为您正在尝试创建此类的实例:

val acc = new ChecksumAccumulator

只有这样才能实现:

object ChecksumAccumulator { ... }

查看有关完全相同的代码示例的答案。缺少class ChecksumAccumulator的定义:

https://stackoverflow.com/a/8681616/5100014

请务必将其粘贴到object ChecksumAccumulator

的同一文件中