Solved sbcl oop program does not work, error undefined variable

The following program does not work,
Code:
(load "~/quicklisp/setup.lisp")
(declaim (optimize (speed 3) (safety 3)))
(defclass counter ()
    ((x :initarg :x :accessor x)))
(defmethod setx (c x2)
    (setf (x c) x2))
(defmethod add1 (c)
    (setf (x c) (+ 1 (x c))))
(defmethod getx (c)
    (x c))
(defun main ()
    (defvar c (make-instance 'counter :x 3 ))
    (setx c 5)
    (add1 c)
    (print (x c)))
(sb-ext:save-lisp-and-die "test.exe" :toplevel #'main :executable t)
 
Code:
(declaim (optimize (speed 3) (safety 3)))
(defclass counter ()
    ((x :initarg :x :accessor x)))
(defmethod setx (c x2)
    (setf (x c) x2))
(defmethod add1 (c)
    (setf (x c) (+ 1 (x c))))
(defmethod getx (c)
    (x c))
(defun main ()
    (let ((c (make-instance 'counter :x 3)))
      (setx c 5)
      (add1 c)
      (print (x c))))
(sb-ext:save-lisp-and-die "test.exe" :toplevel #'main :executable t)

You probably want to compile your code before saving an image.
 
Commenting is out and running :
sbcl --script test.lisp
gives :
Code:
; file: /SSD/home/x/Src/oopme/sbcl_error/test.lisp
; in: DEFUN MAIN
;     (X C)
; 
; caught WARNING:
;   undefined variable: COMMON-LISP-USER::C

;     (ADD1 C)
; 
; caught WARNING:
;   undefined variable: COMMON-LISP-USER::C

;     (SETX C 5)
; 
; caught WARNING:
;   undefined variable: COMMON-LISP-USER::C
; 
; compilation unit finished
;   Undefined variable:
;     C
;   caught 3 WARNING conditions
 
Back
Top