为什么我不传递任何“太多输入参数”错误?

时间:2021-06-19 16:40:44

I am working on some simple object-oriented code in MATLAB. I am trying to call one of my class methods with no input or output arguments in its definition.

我正在研究MATLAB中一些简单的面向对象的代码。我试图调用我的类方法之一,其定义中没有输入或输出参数。

Function definition:

function roll_dice

Function call:

obj.roll_dice;

When this is executed, MATLAB says:

执行此操作时,MATLAB说:

??? Error using ==> roll_dice
Too many input arguments.

Error in ==> DiceSet>Diceset.Diceset at 11
obj.roll_dice;
(etc...)

Anyone have any ideas what could be causing it? Are there secret automatic arguments I'm unaware that I'm passing?

任何人都有任何想法可能导致它?有没有秘密的自动参数我不知道我在路过?

2 个解决方案

#1


When you make the call:

当你打电话时:

obj.roll_dice;

It is actually equivalent to:

它实际上相当于:

roll_dice(obj);

So obj is the "secret" automatic argument being passed to roll_dice. If you rewrite the method roll_dice to accept a single input argument (even if you don't use it), things should work correctly.

所以obj是传递给roll_dice的“秘密”自动参数。如果您重写方法roll_dice以接受单个输入参数(即使您不使用它),事情应该正常工作。

Alternatively, if you know for sure that your method roll_dice is not going to perform any operations on the class object, you can declare it to be a static method as Dan suggests.

或者,如果您确定您的方法roll_dice不会对类对象执行任何操作,则可以将其声明为静态方法,如Dan所建议的那样。

For more information on object-oriented programming in MATLAB, here's a link to the online documentation.

有关MATLAB中面向对象编程的更多信息,请参阅联机文档的链接。

#2


I believe you can also get around this by declaring roll_dice to be a static method.

我相信你也可以通过声明roll_dice是一个静态方法来解决这个问题。

#1


When you make the call:

当你打电话时:

obj.roll_dice;

It is actually equivalent to:

它实际上相当于:

roll_dice(obj);

So obj is the "secret" automatic argument being passed to roll_dice. If you rewrite the method roll_dice to accept a single input argument (even if you don't use it), things should work correctly.

所以obj是传递给roll_dice的“秘密”自动参数。如果您重写方法roll_dice以接受单个输入参数(即使您不使用它),事情应该正常工作。

Alternatively, if you know for sure that your method roll_dice is not going to perform any operations on the class object, you can declare it to be a static method as Dan suggests.

或者,如果您确定您的方法roll_dice不会对类对象执行任何操作,则可以将其声明为静态方法,如Dan所建议的那样。

For more information on object-oriented programming in MATLAB, here's a link to the online documentation.

有关MATLAB中面向对象编程的更多信息,请参阅联机文档的链接。

#2


I believe you can also get around this by declaring roll_dice to be a static method.

我相信你也可以通过声明roll_dice是一个静态方法来解决这个问题。