🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.objective-c
3 messages
3 total messages Started by Karthic V. Rao-Thallikar Fri, 18 Dec 1992 18:31
problem with similar instance method names
#312
Author: Karthic V. Rao-Thallikar
Date: Fri, 18 Dec 1992 18:31
200 lines
4404 bytes
To all Objective-C Guru's:
I am starting to write code in ObjecC. I am familiar with 'C'.
My understanding of OOP is that if there are two similar classes eg  
Integer and Float the user can have the facility of using similarly named  
instance methods to operate on these classes. Just to try this out i coded  
the following lines. But the compiler reports errors for the 'initialize'  
and 'incrementBy' methods, and used the method defined for the Integer  
Class.
Please do reply the correct procedure.
I am using NeXt 3.0, and NeXt's ObjectiveC version that comes with the 3.0  
version.
Thank you all in advance.
Following is the code.
They are in different file but I have 'cat' them for convinience.
The error from the compiler is appended at the end for reference.

//Test program  for late bindind
#import "Integer.m"
#import "Float.m"
#import "Fraction.m"
#import <stdio.h>

main()
{
	int i;
	char choice;
	int value;
	int num,den;
	float r;
	FILE *f;
	
	id data[5]; //Late binding
	
	f = fopen("choices.txt","r");
	
	for( i = 0; i < 5; i++){
		choice = fgetc(f);
		
		if( choice == 'i' || choice == 'I' ){
			printf("\n What is the value:");
			scanf("%d", &value);
			data[i] = [Integer new:value];
		}
		else if( choice == 'r' || choice == 'R'){
			printf("\n what is the numerator?");
			scanf("%d", &num);
			printf("\n what is the denominator?");
			scanf("%d", &den);
			data[i] = [Fraction new:num denominator:den];
		}
		else{
			printf("\n What is the value:");
			scanf("%f",&r);
			data[i] = [Float new:r ];
		}
	}// for
	
	// Increment each value
	
	for( i = 0; i < 5; i++)
		[data[i] incrementBy:10];
	
	
	for( i = 0; i < 5; i++)
		[data[i] print];
	
}// End of main	

// This is a different file but for mailing purposes i have joined it to  
the same file

#import "Integer.h"
#import <stdio.h>

@implementation Integer 

+new:(int)anInteger
{
	id newInstance = [Integer alloc];
	[newInstance initialize:anInteger];
	return newInstance;
}

-initialize:(int)anInt
{
	[super init];
	value = anInt;
	return self;
}

-incrementBy:(int)aValue
{
	value += aValue;
	return self;
}

-print
{
	printf("%d\n", value);
	return self;
}

@end



// This is a different file but for mailing purposes i have joined it to  
the same file

#import "Float.h"

@implementation Float

+new:(float)aFloat
{
	id newInstance = [Float alloc];
	[newInstance initialize:aFloat];
	return newInstance;
}

-initialize:(float)aValue
{
	[super init];
	value = aValue;
	return self;
}

-incrementBy:(float)aValue
{
	value += aValue;
	return self;
}

-print
{
	printf("%f\n",value);
}

@end



// This is a different file but for mailing purposes i have joined it to  
the same file

#import  <objc/Object.h>


@interface Integer: Object
{
	int value;
}

+new:(int)anInteger;

-initialize:(int)anInt;

-incrementBy:(int)aValue;

-print;

@end



// This is a different file but for mailing purposes i have joined it to  
the same file

#import <objc/Object.h>

@interface Float:Object
{
	float value;
}

+new:(float)aFloat;

-initialize:(float)aValue;

-incrementBy:(int)aValue;

-print;

@end;

errors are:
> cc test.m
Float.m: In method `new:'
In file included from test.m:4:
Float.m:8: warning: multiple declarations for method `initialize:'
Integer.h:11: using `-initialize:(int)anInt'
Float.h:10: also found `-initialize:(float)aValue'
Float.m: In method `incrementBy:'
Float.m:20: conflicting types for `-incrementBy:(float)aValue'
Float.h:12: previous declaration of `-incrementBy:(int)aValue'
test.m: In function `main':
test.m:46: warning: multiple declarations for method `incrementBy:'
Integer.h:13: using `-incrementBy:(int)aValue'
Float.m:20: also found `-incrementBy:(float)aValue'
>
Re:similar instance method names,THANKS
#290
Author: Karthic V. Rao-Thallikar
Date: Mon, 21 Dec 1992 17:32
6 lines
921 bytes
Thanks to all persons who replied to my request for help. I spent a lot of  
time before i posted it. I thought that there must be some method to  
implement methods with same names but different arguments. 
I appreciate everybody who replied to me.
Thank you all
Karthic
Re: problem with similar instance method names
#291
Author: tuncay
Date: Mon, 21 Dec 1992 20:44
63 lines
2917 bytes
hi,
In article <1992Dec18.183133.6855@ennews.eas.asu.edu>,
raothall@iienext1.cim.eas.asu.edu (Karthic V. Rao-Thallikar) says:
>

I ran into the same problem with compiling the same program on the NeXT
platform.  My advisor solved the problem by:first including the header files of
the classes istead of .m files, then, compiling the .m files all together.

cc -ObjC Integer.m Float.m Fraction.m theMain.m
rm
>To all Objective-C Guru's:
>I am starting to write code in ObjecC. I am familiar with 'C'.
>My understanding of OOP is that if there are two similar classes eg
>Integer and Float the user can have the facility of using similarly named
>instance methods to operate on these classes. Just to try this out i coded
>the following lines. But the compiler reports errors for the 'initialize'
>and 'incrementBy' methods, and used the method defined for the Integer
>Class.
>Please do reply the correct procedure.
>I am using NeXt 3.0, and NeXt's ObjectiveC version that comes with the 3.0
>version.
>Thank you all in advance.
>Following is the code.
>They are in different file but I have 'cat' them for convinience.
>The error from the compiler is appended at the end for reference.
>
>//Test program  for late bindind
>#import "Integer.m"
>#import "Float.m"

Here, import the header files: Integer.h, Float.h and Fraction.h

>#import "Fraction.m"
>#import <stdio.h>
>
>errors are:

>> cc test.m

you have to use -ObjC option when you compile Objective C code in the NeXT
platform, as far as I know.  Use the following command:

    cc -ObjC Integer.m Float.m Fraction.m test.m -o test

>Float.m: In method `new:'
>In file included from test.m:4:
>Float.m:8: warning: multiple declarations for method `initialize:'
>Integer.h:11: using `-initialize:(int)anInt'
>Float.h:10: also found `-initialize:(float)aValue'
>Float.m: In method `incrementBy:'
>Float.m:20: conflicting types for `-incrementBy:(float)aValue'
>Float.h:12: previous declaration of `-incrementBy:(int)aValue'
>test.m: In function `main':
>test.m:46: warning: multiple declarations for method `incrementBy:'
>Integer.h:13: using `-incrementBy:(int)aValue'
>Float.m:20: also found `-incrementBy:(float)aValue'
>>
The above two changes should fix the problem.  I like PINSON/WIENER'S book
a lot despite the fact that I could not work some of their code on
the machine that I use. It is a NeXT, too.

Have fun.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads