简体   繁体   中英

Implicit Declaration and Error Inserting a Function in XCode

First of all, Happy New Year to anybody out there reading this!

I have an i-phone app I am working on and wanted to make an enhancement to an IBACTION (GetNextElement) which is called from a button tap. I wanted to call a function (get_index_to_use) rather than insert the code in GetNextElement. When I call the function in my SymbolTest.m file, I get a warning "implicit declaration of function get_index_to_use" .

I get an error on the build that I don't see unless I go into Build Results where I see the following:

Ld build/Debug-iphonesimulator/elements1.app/elements1 normal i386 cd /Users/dad/Documents/elements1 setenv MACOSX_DEPLOYMENT_TARGET 10.6 setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -L/Users/dad/Documents/elements1/build/Debug-iphonesimulator -F/Users/dad/Documents/elements1/build/Debug-iphonesimulator -filelist /Users/dad/Documents/elements1/build/elements1.build/Debug-iphonesimulator/elements1.build/Objects-normal/i386/elements1.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework Foundation -framework UIKit -framework CoreGraphics -o /Users/dad/Documents/elements1/build/Debug-iphonesimulator/elements1.app/elements1

Undefined symbols: "_get_index_to_use", referenced from: -[SymbolTest GetNextElement:] in SymbolTest.o ld: symbol(s) not found collect2: ld returned 1 exit status

I'm not familiar with SymbolTest.o.

I have my function in the code before GetNextElement. I tried to put it in SymbolTest.h. I tried to put it in GetNextElement but couldn't get that to work either. I have tried - and +. The function looks like this (eventually I want to change how I calculate the index, but for now I am just incrementing it to get the function to work):

- (void)get_index_to_use {
    el_tbl_idx++;
}   

Any help would be appreciated. Thanks.

here is your problem, you have declared a method and then tried to call like a C function.

you will want to use (C style function)...

void get_index_to_use (void)
{ el_tbl_idx++; }

then call with:

get_index_to_use()

or

- (void) get_index_to_use
{el_tbl_idx++; }

and call with:

[self get_index_to_use];

or more generically:

[someObject get_index_to_use];

but it is generally a good idea to stick to naming conventions, one of which, is if you method starts with get, it should be a getter and return something. The following looks more Obj-C.

-(NSUInteger)getIndex {
     el_tbl_idx+=1; // or ++ or = x+1;
     return el_tbl_idx; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM