Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Classes in functions

    Hi
    According to the code below it seems like you can not use classes as arguments to or inside functions.

    Is that true? Or am I doing something wrong?

    If it is true is there then a workaround for it?

    Code:
    cls
    mata
        mata clear
    
        
        class coord {
                real scalar    x, y
                real scalar    length(), angle()
        }
    
            real scalar coord::length()
            {
                    return(sqrt(x^2 + y^2))
            }
    
            real scalar coord::angle()
            {
                    return(atan2(y, x)*360/(2*pi()))
            }
    
        
        void test1(class coord scalar c)
        {
            return(c.length())
        }
    
        void test2(real scalar x, y)
        {
            real scalar l
            class coord scalar c
            
            c = coord()
            c.x = x
            c.y = y
            l = c.length()
            return(l)
        }
    
        c = coord()
        c.x = 4
        c.y = 3
        c.length()
        
        test1(c)
        /* From the Results window
        :         test1(c)
                         test1():  3258  l[1,1] found where void required
                         <istmt>:     -  function returned error
        r(3258);
        */
        
        test2(5, 12)
        /* From the Results window
        :         test2(5, 12)
                         test2():  3258  l[1,1] found where void required
                         <istmt>:     -  function returned error
        r(3258);
        */
    end
    Kind regards

    nhb

  • #2
    Your test1() and test2() are declared as returning void but should be declared as returning real scalar.

    Comment


    • #3
      Ah
      Now everything is working , also:
      Code:
      :     class coord scalar test4(class coord scalar c, real scalar x, y)
      >     {
      >         c.x = x
      >         c.y = y
      >         return(c)
      >     }
      
      :     
      :     c = coord()
      
      :     c = test4(c, 5, 12)
      
      :     c.length()
        13
      So it was just me.

      Thank you very much
      Kind regards

      nhb

      Comment

      Working...
      X