Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Undefined operator '>' for input arguments of type 'BasicClass'

1 Ansicht (letzte 30 Tage)
Maciej Satora
Maciej Satora am 1 Sep. 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
Hey, I want to write a simple class that will calculate recursive some values. Then, when I want to call my function, I got the report: Undefined operator '>' for input arguments of type 'BasicClass' Any ideas how can I repair this? I'm pretty new with the object oriented programming...
classdef BasicClass
properties
Value
end
methods
function [Result] = r (Value)
if Value > 0
Result = r(Value - 1) * 1000+2;
else
Result =2;
end
end
end
end

Antworten (2)

Steven Lord
Steven Lord am 1 Sep. 2017
The input argument to the r method of the BasicClass class will be an instance of BasicClass. You will need to access the property of that object explicitly; there's no implicit "Because this identifier matches the name of a property, we'll use the value of that property on the instance of the class in this method" behavior.
While you could make a new BasicClass object to perform the recursive call to the r method, I would instead use a class-related function that is not a method to do that recursion. Put all of the next block of code into a file named BasicClass.m.
classdef BasicClass
properties
Value
end
methods
function [Result] = r (myobj)
Result = recurseComputeR(myobj.Value);
end
end
end
function Result = recurseComputeR(myobjValue)
if myobjValue > 0
Result = recurseComputeR(myobjValue-1)*1000+2;
else
Result = 2;
end
end
Use this class like:
obj = BasicClass;
obj.Value = 2;
answer = r(obj)
You should receive 2002002 in the variable answer.

Maciej Satora
Maciej Satora am 2 Sep. 2017
Thank You sir! It helped a lot. Is it like creating a function that is reference to another one?

Diese Frage ist geschlossen.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by