A simple question (pointer, i think)

Discussion in 'Mac Programming' started by carvalhaes133, Aug 27, 2010.

  1. carvalhaes133 macrumors newbie

    Joined:
    Aug 27, 2010
    #1
    Hi!
    Someone knows whats the problem whit this obj-c code?

    Code:
    //
    //  main.m
    //  Untitled
    
    #import <Cocoa/Cocoa.h>
    #include <stdio.h>
    #import <objc/Object.h>
    
    // aqui comeca a definicao das interfaces
    @interface Cat: Object
    
    -(void) miau;
    
    @end
    
    // aqui comecam as implementacoes dos metodos
    @implementation Cat
    
    -(void) miau
    {
    	printf("Miaau!\n");
    }
    
    @end
    int main( int argc, const char *argv[] )
    {
    	
    	Cat *myCat = [[Cat alloc] init];
    	
    	[myCat miau];
    	
    	[myCat free];
    		
    	return 0;
    }
    i just wanna see "miaau!" at the console...
    thanks!
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    So what do you see? Any compiler errors and / or warnings?
     
  3. ranguvar macrumors 6502

    Joined:
    Sep 18, 2009
    #3
    Code:
    #import <Foundation/Foundation.h>
    
    @interface Cat : NSObject {
    	//
    }
    
    - (void)miau;
    
    @end
    
    
    @implementation Cat
    
    
    - (void)miau {
    	printf("Miaau!\n");
    }
    
    
    @end
    
    
    int main(int argc, const char *argv[]) {
    	Cat *myCat = [[Cat alloc] init];
    	[myCat miau];
    	[myCat release];
    	return 0;
    }
    Fixed that for you. If you really want to develop without using Cocoa (as the
    Code:
    #import <objc/Object.h>
    indicates, my solution is of course not correct.

    It doesn't look like you understood Objective-C. Read Apple's "Introduction to the Objective-C Language".
     
  4. carvalhaes133 thread starter macrumors newbie

    Joined:
    Aug 27, 2010
    #4
    ok thanks, i started to try learn obj-c yesterday.

    my objective is program to iphone, but i want to learn the basics here (mac programming)...

    1 - at line

    Cat *myCat = [[Cat alloc] init];
    i received a warning message: "'cat' may not respond to '+alloc'"

    2 - at line
    [myCat free];
    i received a warning message: "'cat' may not respond to '-free'"

    3 - and at console:

    2010-08-27 12:50:29.163 Untitled[200:a0f] *** NSInvocation: warning: object 0x100002078 of class 'Cat' does not implement methodSignatureForSelector: -- trouble ahead
    2010-08-27 12:50:29.165 Untitled[200:a0f] *** NSInvocation: warning: object 0x100002078 of class 'Cat' does not implement doesNotRecognizeSelector: -- abort
    sharedlibrary apply-load-rules all
    (gdb)

    why the console don't show 'miauu'??
    but thanks for your solution, but i think that i need another one
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    You should copy a working program and modify it one step at a time. Make sure it still compiles and works at every step.

    You have some serious errors in your code. The simplest way to address them is to start with something that works. When you have more experience, then you can write classes without starting from a working example.

    If you're not working from a book, you should be. If you are working from a book, exactly what book (title, author edition)?
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    This happens from time to time. You are using an outdated book or website to learn from.

    Cat doesn't respond to +alloc because it is a subclass of Object, not NSObject
    -free is suitably replaced by -release
     
  7. ranguvar macrumors 6502

    Joined:
    Sep 18, 2009
    #7
    You must have messed up. I actually compiled and ran the code before I posted it, and it does work. If you just copy the code I supplied you with into your main.m, it will work.
     

Share This Page