所以我刚开始学习 Crystal,因为我喜欢 Ruby 和 C,但我还不能掌握语法。我想我已经很接近了,但我被这个错误困住了。
没有重载匹配 Person 类型的 'Array(Person)#+'
重载是:
- 阵列(T)#+(其他:阵列(U))
people += Person.new("Person#{id}")
这是代码。
class Person
def initialize(name : String)
@name = name
@age = 0
end
def name
@name
end
def age
@age
end
end
people = [] of Person
counter = 0
id = 0
loop do
id+=1
people += Person.new("Person#{id}")
counter+=1
break if counter = 5
end
puts(people)
我做错了什么? 提前致谢!
最佳答案
您正试图将一个数组和一个人放在一起。但是你可以添加 Array to Array only.
解决它,你应该使用Array#<<
, 像这样:
people << Person.new("Person#{id}")
注意:检查您的第 25 行,它应该是 break if counter == 5
关于crystal-lang - 没有重载与 Person 类型匹配 'Array(Person)#+',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48751355/