【发布时间】:2014-02-19 19:48:28
【问题描述】:
> foo <- structure(list(one=1,two=2), class = "foo")
> cat(foo)
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
好的,我将它添加到通用猫中:
> cat.foo<-function(x){cat(foo$one,foo$two)}
> cat(foo)
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
没有骰子。
【问题讨论】:
-
如果函数一开始就不是通用的,那么仅仅编写一个名为 function.class 的新函数并没有多大作用。话虽如此,我还没有尝试过,但我感觉 cat 有 ... 作为它的第一个参数可能会导致一些并发症
-
直接调用 cat.foo 有什么明显的错误吗?
cat.foo(foo) -
一个快捷的解决方案是定义
print.foo。 -
> print.foo<-function(x){cat(foo$one,foo$two)} > cat(foo) Error in cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') cannot be handled by 'cat' -
我认为 agstudy 暗示您使用 print 而不是 cat。为什么您需要为此使用 cat ?
print已经是通用的,因此为自己的类编写自己的方法很容易。