tags:

views:

1294

answers:

4

Hey there, I'm sort of new to Objective-C, and well, programming in general. I have a little experience in C# and c++, to give you a bit of a background. I'm using Xcode to create a simple application that adds 2 fields and returns a sum, and I've narrowed it down to one error:

"Nested functions are disabled, use -fnested-functions to re-enable"

Here's the bit of code I'm having trouble with, any suggestions would be greatly appreciated:

-(IBAction)click:(id)sender;
{
int main (int argc, const char *argv[]){
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  int sum;

  sum = myInt1, myInt2;
  NSLog (name, @", the answer is %i", sum);
  [pool drain];

  return 0;
 }
}
+3  A: 

What you are trying to do here is something which is not generally allowed in C languages. You, in general, cannot create a function inside of another one, like you can in languages like Pascal and JavaScript. You should remove the main stuff from inside click: and just keeps the lines

int sum;

sum = myInt1, myInt2;
NSLog (name, @", the answer is %i", sum);

On a broader, note, though, why are you trying to create a main inside of click:? In Objective-C you normally use main() just to create any root-level objects and instantiate the autorelease pool. Assuming you're working with Xcode on a Mac, if you use one of the templates to get started it should take care of all the details for you of creating main. I'd suggest taking a look at Apple's Cocoa tutorials to help you get a feel for how a typical Objective-C application is structured.

Gordon Worley
Thank you very much for your advice Gordon, I have picked up a few books and have been doing a lot of reading up on the subject lately, as I've said, I'm quite new to general C. So, still trying to work out the kinks by doing a few very basic apps and learning the ropes.Thanks again for the expedient response :)
jxd215
A: 

Wooble has it right you cant define the main function inside the click method!

ennuikiller
+1  A: 

ok, well.. I see you already got a button and an action for it, named "click".. you could write it like this:

-(IBAction)click:(id)sender;
{
  int sum;

  sum = myInt1, myInt2;
  NSLog (name, @", the answer is %i", sum);
 }
}

Or, create a simple Foundation Tool project and put this in:

int main (int argc, const char *argv[]){
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  int sum;

  sum = myInt1, myInt2;
  NSLog (name, @", the answer is %i", sum);
  [pool drain];

  return 0;
 }

the problem you're having is that you tried declaring the main function inside the click method.. it doesn't have any sense. So if you're a beginner, try learning using foundation tool projects, and not gui projects.

Woofy
There's no reason beginners can't learn with GUI projects. I'm sure there's no tutorial out there that told him to do this.
Chuck
In my opinion regarding any programming language, it's best to start with console programs to learn the language syntax, features, possibilities, and then move on to gui... if you start with gui projects right from the beginning, you'll find out that you don't know how to make simple things because you don't even know the language itself and what you can do with it. And I'm speaking from my own experience because it is a proven technique as I managed to learn almost ten programming languages using this technique.
Woofy
+1  A: 

You have somehow stuck the main() function inside your click: method, which doesn't make any sense. You couldn't stick main() inside a class member function in C++ or a method in C#, and this is exactly the same thing. main() should be its own function (Xcode should have provided a ready-made main.m file that you don't need to change), and click: should contain only the functionality that you want it to perform.

Chuck