views:

372

answers:

2

i have a struct HLRange with two CGFloat's

struct HOLRange
{
CGFloat min;
CGFloat max;
};
typedef struct HOLRange HOLRange;

but how do i make a function like HLRangeMake(1,2); .. like CGRectMake?

--EDIT--

my header file

#import <Foundation/Foundation.h>

struct HOLRange
{
    CGFloat min;
    CGFloat max;
};
typedef struct HOLRange HOLRange;

HOLRange HOLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
 }

@interface Structs : NSObject {

}

@end


error message: ld: duplicate symbol _HOLRangeMake in             /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/base1AppDelegate.o and /Users/Documents/projects/iphone/test/catalog/base1/build/base1.build/Debug-iphoneos/base1.build/Objects-normal/armv6/main.o
+3  A: 
HOLRange HLRangeMake(CGFloat min, CGFloat max) {
    HOLRange range;
    range.min = min;
    range.max = max;
    return range;
}
Ole Begemann
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1, if i add that code
Andy Jacobs
What's the compiler's error message? Where exactly did you put that code? It must be outside a class implementation. Did you also include a corresponding function declaration in the header file? please edit your question and add more info.
Ole Begemann
i have edited it
Andy Jacobs
The linker is complaining about a duplicate symbol because you are importing the header file in more than one other file and this causes the function to be defined multiple times. Put just the function declaration in the header file: `HOLRange HOLRangeMake(CGFloat min, CGFloat max);` and move the function definition into the implementation file.
Ole Begemann
thx it works now!
Andy Jacobs
+2  A: 

You can see CGRectMake source in CGGeometry.h so you can do the same:

CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
  CGRect rect;
  rect.origin.x = x; rect.origin.y = y;
  rect.size.width = width; rect.size.height = height;
  return rect;
}

Edit: You must either define your function as inline or move its implementation to .m file. You're getting linker errors as you function becomes defined in every compile unit that imports HoleRange.h (?) header.

Vladimir