Newsgroups: comp.lang.objective-c
Subject: problem with similar instance method names
From: raothall@iienext1.cim.eas.asu.edu (Karthic V. Rao-Thallikar)
Date: 18 Dec 92 18:31:33 GMT
Distribution: comp.lang.objective-c
Organization: Arizona State University

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'
>
Newsgroups: comp.lang.objective-c