Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

carvalhaes133

macrumors newbie
Original poster
Aug 27, 2010
9
0
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!
 
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".
 
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
 
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)?
 
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
 
why the console don't show 'miauu'??
but thanks for your solution, but i think that i need another one

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.