Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 1, 2011, 03:12 PM   #1
spt
macrumors member
 
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, 03: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, 03: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, 04:13 PM   #4
spt
Thread Starter
macrumors member
 
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, 04:32 PM   #5
chown33
macrumors 603
 
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, 04:52 PM   #6
gnasher729
In Time-Out
 
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

Similar Threads
thread Thread Starter Forum Replies Last Post
Use of Undeclared identifier "initwithnibname" and "receivememorywarning" MozzaCheese iPhone/iPad Programming 1 Apr 22, 2014 08:50 AM
Undeclared Identifier (Objective-C) G-Man118 iPhone/iPad Programming 3 Mar 26, 2014 01:52 AM
Strange model identifier yjchua95 iMac 2 Nov 2, 2013 04:43 AM
Undeclared identifier in UIAlertView Kapthehat iPhone/iPad Programming 2 Sep 2, 2013 07:19 AM
12,5 will be the model identifier of the new iMac Puevlo iMac 5 Sep 5, 2012 12:35 AM

Forum Jump

All times are GMT -5. The time now is 10:08 PM.

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

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC