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
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"
Ivan Handler up
Ivan Handler up 2017년 1월 2일
I figured out the problem. This is a rather clunky implementation of OOP, but I am starting to get the hang of it.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile 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!

Translated by