Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 1, 2011, 02:12 PM   #1
spt
macrumors newbie
 
Join Date: Oct 2009
use of undeclared identifier

I would like to build a simple interface around some c-code I have. I've spend over two hours now on a seemingly simple problem with Objective C.

It simply seems impossible to call an Objective C function from within a normal c function. If I do, for example:

void changeInfo()
{
[TextField setIntValue:test];
}

I get an error that says "Use of undeclared identifier 'TextField'". Whereas if this piece of code would be like this:

- (void) changeInfo
{
[TextField setIntValue:test];
}

... it would work perfectly fine.

What am I doing wrong...?
spt is offline   0 Reply With Quote
Old Aug 1, 2011, 02:19 PM   #2
kpua
macrumors 6502
 
Join Date: Jul 2006
Your 'TextField' variable isn't global—it's an instance variable of your class.

Objective-C methods have two implicit parameters that you don't see directly in the code: 'self' (a pointer to the object the method is called on) and '_cmd' (the selector of the method being called). When you access 'TextField', the code that gets generated is actually 'self->TextField'.

In a plain C function, there can be no implicit parameters, so even if your C function is defined inside your @implementation, it won't be able to find 'TextField', because it doesn't even know what 'self' is.

One thing I often do is just make an explicit 'self' parameter in plain C functions, and then access the ivars explicitly with 'self->TextField'. The compiler won't complain about @private or @protected ivars as long as the function definition is within the @implementation of your class.
kpua is offline   0 Reply With Quote
Old Aug 1, 2011, 02:29 PM   #3
GorillaPaws
macrumors 6502a
 
GorillaPaws's Avatar
 
Join Date: Oct 2003
Location: Richmond, VA
Is TextField an iVar? If so, it should be textField because the leading character should only be caps if it's a Class name, not an instance of a class.

The issue you're having is one of the scope of your variables. In your function example, since you haven't passed in a reference to your textField object, your code won't be able to access it. For example, this should work:
Code:
void changeInfo( NSTextField *aTextField )
{
     [aTextField setIntValue:test];
}
Whereas the method version will work if "aTextField" is declared as an ivar of the Class you're working in. All instance methods have access to the ivars of the instances of their class.
__________________
How to ask good programming questions: Getting Answers
GorillaPaws is offline   0 Reply With Quote
Old Aug 1, 2011, 03:13 PM   #4
spt
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Thanks a lot for your comments, it now works!

Code:
void changeInfo( NSTextField *aTextField )
{
     [aTextField setIntValue:test];
}
Using a global did the trick.

The above suggestion would be quite complex in this specific case, as the c-program involves starting a lot of threads using the pthread library. The thread functions thus can't easily be started with an argument.
spt is offline   0 Reply With Quote
Old Aug 1, 2011, 03:32 PM   #5
chown33
macrumors 601
 
Join Date: Aug 2009
Quote:
Originally Posted by spt View Post
The above suggestion would be quite complex in this specific case, as the c-program involves starting a lot of threads using the pthread library. The thread functions thus can't easily be started with an argument.
The function prototype for pthread_create():
Code:
     int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void
                        *(*start_routine)(void *), void *arg)
The blue-hilited part declares that start_routine is a function with a single parameter, whose type is pointer-to-void. The red-hilited parameter is the actual argument value to pass to the function.

From the man-page for pthread_create():
The thread is created executing start_routine with arg as its sole argument.
chown33 is offline   0 Reply With Quote
Old Aug 1, 2011, 03:52 PM   #6
gnasher729
macrumors G4
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by spt View Post
Thanks a lot for your comments, it now works!

Code:
void changeInfo( NSTextField *aTextField )
{
     [aTextField setIntValue:test];
}
Using a global did the trick.

The above suggestion would be quite complex in this specific case, as the c-program involves starting a lot of threads using the pthread library. The thread functions thus can't easily be started with an argument.
Reading up on "blocks" and "Grand Central Dispatch" will safe you an awful, awful lot of headaches. Us as well.
gnasher729 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Future use of Leopard (not even SnowLeopard) with iCal+iPhone ParisParamus iCloud and Apple services 0 Nov 20, 2011 10:51 AM
Use of undeclared identifier 'dvController' lJoct03 iPhone/iPad Programming 5 Oct 18, 2011 08:13 PM
How to create a custom property and identifier? mandude iPhone/iPad Programming 2 Aug 12, 2011 01:37 PM
Any use for undeclared @implementations? zippyfly Mac Programming 3 Sep 29, 2010 02:33 AM
Identifying program that make use of multiple cores skerfoot Mac Pro 1 Jun 23, 2010 12:49 PM


All times are GMT -5. The time now is 05:57 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC