Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.objective-c > #194

Re: Objective C program doesn't compile

From Don Bruder <dakidd@sonic.net>
Newsgroups comp.lang.objective-c
Subject Re: Objective C program doesn't compile
Date 2015-11-11 02:14 -0800
Organization Chaotic Creations Unlimited
Message-ID <n1v492$up$1@dont-email.me> (permalink)
References <a5d7b3bf-a215-4dbf-9888-dafcd52fba9f@googlegroups.com>

Show all headers | View raw


In article <a5d7b3bf-a215-4dbf-9888-dafcd52fba9f@googlegroups.com>,
 modelling.data@gmail.com wrote:

> Hello!
> 
> Please, take a look at the program I typed in from the textbook Programming
> in C by S. Kochan. I am new to all this, and had no idea how to compile
> such programs, the book doesn't cover this topic either. Given I have read 
> almost the whole book which is dedicated to C programming, I have tried to 
> use
> my Terminal to compile a program. Unfortunately, nothing I tried worked. 
> 
> I post my steps, as well as warning messages I get.
> 
> I would be grateful for explanation on what I am doing wrong. I work on Mac 
> OS 10.7.5, Xcode 4.6.3, Terminal 2.2.3.
> 
> 1) I created a file with the command:
> 
> touch prog18-2.m
> 
> Initially, I have tried usual touch prog18-2.c, then I googled and found 
> the information on .m extensions. 
> 
> 2) Program
> 
> / Program to work with fractions - Objective-C version
> 
> #import <stdio.h>
> #import <objc/Object.h>
> 
> //------ @interface section ---------
> 
> @interface Fraction: Object
> {
>   int numerator;
>   int denominator;
> }
> 
> -(void) set_numerator: (int) n;
> -(void) set_denominator: (int) d;
> -(void) print;
> 
> @end
> 
> // ----- @implementation section -------
> 
> @implementation Fraction;
> 
> // getters
> 
> -(int) numerator
> {
>   return numerator;
> }
> 
> -(int) denominator
> {
>   return denominator;
> }
> 
> //setters
> 
> -(void) set_numerator: (int) num
> {
>    numerator = num;
> }
> 
> -(void) set_denominator: (int) denom
> {
>   denominator = denom;
> }
> 
> //other
> -(void) print
> {
>   printf("The value of the fraction is %i/%i\n", numerator, denominator);
> }
> 
> @end
> 
> //-------- program section -----------
> 
> int main(void)
> {
>   Fraction* my_fract;
>   my_fract = [Fraction new];
> 
>   [my_fract set_numerator: 1];
>   [my_fract set_denominator: 3];
> 
>   printf("The numerator is %i, and teh denominator is %i\n", [my_fract 
>   numerator], [my_fract denominator]);
> 
>   [my_fract print];
> 
>   [my_fract free]; //frees the memory that was used by Fraction object
> 
> return 0;
> }
> 
> 3) I compiled it with:
> 
> gcc -framework Foundation prog18-2.m -o prog18-2
> 
> 4) The Terminal generated the following:
> 
> prog18-2.m: In function 'main':
> prog18-2.m:58: warning: 'Fraction' may not respond to '+new'
> prog18-2.m:58: warning: (Messages without a matching method signature
> prog18-2.m:58: warning: will be assumed to return 'id' and accept
> prog18-2.m:58: warning: '...' as arguments.)

Means exactly what it says - You're trying to create a new Fraction 
object (via "[Fraction new]") but your Fraction class doesn't contain a 
"new" class method. I see from looking at the code that it also lacks 
any form of "init" or "initWith..." methods, as well. 

> prog18-2.m:71: warning: 'Fraction' may not respond to '-free'

Exactly the same problem - You have no "free" instance method for class 
Fraction.

If that's code you've keyed from a book, the book is garbage, and the 
author needs to be slapped upside the head for being an incompetent 
moron. Especially if you've gotten to chapter 18 and such basic material 
as initializers hasn't been covered well enough for you to know it. I 
assume (perhaps foolishly) that the book you're using has at least 
taught you the difference between a "class" method (such as +new) and an 
"instance" method (such as "-free")? The code you show could be massaged 
into working, but as far as how to write something that at least 
approximates decent Objective-C code, it's a great example of how NOT to 
do it!

> 
> Should I use .m or .c for such programs? Am I using a correct compilation
> command?
> 
> Thank you!

-- 
Security provided by Mssrs Smith and/or Wesson. Brought to you by the letter Q

Back to comp.lang.objective-c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Objective C program doesn't compile modelling.data@gmail.com - 2015-11-10 23:32 -0800
  Re: Objective C program doesn't compile Don Bruder <dakidd@sonic.net> - 2015-11-11 02:14 -0800
    Re: Objective C program doesn't compile modelling.data@gmail.com - 2015-11-11 06:29 -0800
      Re: Objective C program doesn't compile Don Bruder <dakidd@sonic.net> - 2015-11-11 09:04 -0800
      Re: Objective C program doesn't compile "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-11 19:45 +0100
  Re: Objective C program doesn't compile "Pascal J. Bourguignon" <pjb@informatimago.com> - 2015-11-11 21:03 +0100
    Re: Objective C program doesn't compile modelling.data@gmail.com - 2015-11-13 08:19 -0800

csiph-web