Class association has error
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
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
  Adam
      
      
 2016년 11월 23일
				
      편집: Adam
      
      
 2016년 11월 23일
  
			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.
답변 (1개)
  Guillaume
      
      
 2016년 11월 23일
        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
  Guillaume
      
      
 2016년 11월 23일
				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
      
      
 2016년 11월 23일
				
      편집: Adam
      
      
 2016년 11월 23일
  
			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.
참고 항목
카테고리
				Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

