what causes this error? When constructing an instance of class 'circular', the constructor must preserve the class of the returned object.
3 次查看(过去 30 天)
显示 更早的评论
I'm an old Java programmer (actually I program in many languages). I am trying to create some simple classes but get this error (question line). I am enclosing the code that is causing the error when I invoke it.
classdef circular
%This creates a curve based on a previous curve, often just a straight
%line. The parameters are:
%
% n: the number of points in the obj.curve - deduced from obj.curve
% m: the number of twists to this obj.curve
% t: the index into the unit circle
% obj.curve: matrix of points in R3 - the obj.curve itself is the first 3
% columns
% N: the list of normal vectors to the obj.curve in columns 4 to 6
% ON1: list of unit vectors orthogonal to N and ON2 in columns 7 to 9
% ON2: list of unit vectors orthogonal to N and ON1 in columns 10 to 12
% obj.ocurve: output: a matrix of points in R3 - same structure as obj.curve
% Detailed explanation goes here
properties
n = 0;
m = 0;
r = 0
t = 1;
curve = zeros(1,12);
ocurve = zeros(1,12);
end
%
methods
function obj = circular(j, rad, initialcurve)
obj.m = j;
obj.curve = initialcurve;
v = size(obj.curve);
obj.n = v(1);
inc = 2 * pi / obj.n;
obj.t = 0:inc:2*pi - inc;
obj.r = rad;
obj.ocurve = zeros(obj.n,12);
%
for k = 1 : obj.n
% compute the obj.curve
ct = obj.r*cos(obj.m * obj.t(k));
st = obj.r*sin(obj.m * obj.t(k));
oc1 = obj.curve(k,7)*ct + obj.curve(k,10)*st;
oc2 = obj.curve(k,8)*ct + obj.curve(k,11)*st;
oc3 = obj.curve(k,9)*ct + obj.curve(k,12)*st;
e1 = obj.curve(k, 7:9);
e2 = obj.curve(k, 10:12);
p = obj.r*cos(obj.m * obj.t(k))*e1 + obj.r*sin(obj.m * obj.t(k))*e2 + obj.curve(k, 1:3);
obj.ocurve(k, 1:3) = p;
mg = sqrt(oc1^2 + oc2^2 + oc3^2);
%
% compute the normals
nc = obj.m * obj.r * (cos(obj.m * obj.t(k)) * e2 - sin(obj.m * obj.t(k))*e1);
pt = obj.curve(k, 4:6);
obj.ocurve(k, 4:6) = pt + nc;
% compute orthogonal vector 1: circle displacement at t(k)
obj.ocurve(k,7) = oc1 / mg;
obj.ocurve(k,8) = oc2 /mg;
obj.ocurve(k,9) = oc3/mg;
% compute orthogonal vector 2 as C X N / || C X N ||
ox = obj.ocurve(k,2) * obj.ocurve(k,6) - obj.ocurve(k,3) * obj.ocurve(k,5);
oy = obj.ocurve(k,3) * obj.ocurve(k,4) - obj.ocurve(k,1) * obj.ocurve(k,6);
oz = obj.ocurve(k,1) * obj.ocurve(k,5) - obj.ocurve(k,2) * obj.ocurve(k,4);
mg = sqrt(ox^2 + oy^2 + oz^2);
obj.ocurve(k,10) = ox / mg;
obj.ocurve(k,11) = oy / mg;
obj.ocurve(k,12) = oz / mg;
end
obj = obj.ocurve;
end
%
function a = mag(x,y,z)
a = sqrt(x^2 + y^2 + z^2);
end
end
end
2 个评论
per isakson
2017-1-1
What is the intent of
obj = obj.ocurve;
In this line a double vector is assigned to obj, which violates "the constructor must preserve the class of the returned object"
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!