Class association has error

3 views (last 30 days)
shawin
shawin on 23 Nov 2016
Edited: Adam on 23 Nov 2016
I have the code bellow:
Course.m
classdef Course < handle
properties
courseName
stdent
end
properties ( Access= private)
lastIDX=1
end
methods
function obj= Course(courseName, capacity)
obj.courseName= courseName
obj.stdent= cell(capacity,1)
end
function addStudent(obj,stdent)
if obj.lastIDX > numel(obj.stdent)
fprintf(2,'sorry class is full`an');
return
end
obj.stdent(obj.lastIDX)=stdent;
obj.lastIDX= obj.lastIDX +1;
end
function printClass(obj)
fprintf('course name = %s\n ', obj.courseName)
fprintf('Enroled =%d , capacity =%d\n', obj.lastIDX-1, length(obj.stdent));
for i =1:obj.lastIDX-1
fprintf('ID =%d, name = %s\n', i, obj.stdent{i}.name);
end
end
end
end
StudentTest1.m
classdef studentTest1 < handle
properties
name
end
methods
function obj=studentTest1(name)
if nargin > 0
obj.name=name;
end
end
function delete(obj)
fprintf('--Student Destructor: %s\n',obj.name);
end
end
end
I call the objects :
c = Course('AAA',3);
for i = 1 :4
name= fprintf('amro%d',i);
fprintf('Adding student :%s\n', name)
c.addStudent(name);
end
c.printClass()
clear c
but receiving error :
Error using Course
The specified superclass 'handle' contains a parse error, cannot be found on MATLAB's search path or is shadowed by another file with the same name.
Error in TestStudentCourse (line 6) c = Course('AI',3);
any help, please
  1 Comment
Adam
Adam on 23 Nov 2016
Edited: Adam on 23 Nov 2016
Please don't edit away the original question to turn it into a new one when fixing the first problem just leads to another one, it is very confusing and doesn't help any future people coming to the question and looking at the answer provided which no longer makes much sense, relative to the question.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 23 Nov 2016
Well, learn to read error messages. It's telling you that the superclass handel cannot be found. That's because you misspelled it, it should be handle.
classdef Course < handle
  5 Comments
Guillaume
Guillaume on 23 Nov 2016
Learn to debug your program. The simplest way is to issue
dbstop if error.
then run the code. It will break into the debugger at the line where the error occurs. Look at what is in obj.stdent{i}. Most likely it's not a studentTest object. What is it?
Adam
Adam on 23 Nov 2016
Edited: Adam on 23 Nov 2016
I make very heavy use of
doc validateattributes
for the input arguments of all my public functions of classes to ensure that when something gets passed into the function I know exactly what it is (for ease of debugging purposes, but also for documentation) and if I pass the wrong thing in it will give an error with an error message that tells me exactly this rather than sliding down through the code to some potentially obscure error when it actually tries to use the object.
It obviously adds a small amount to your development time and the runtime, but neither is significant to me compared to the value it gives me for debugging and coming back to a class 6 months down the line when I can't remember if theat 2nd input argument was supposed to be a string or a scalar or some object or other of my own class, etc.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!