0

I have 2 class in my program first class is class1 and second class is class2.I want create and initialize global variable in class 1 and to use in class 2 but compiler give me this ERROR XD :

Undefined symbols for architecture i386:
  "_saeid", referenced from:
      -[class2 viewDidLoad] in class2.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I create global variable in class1 and run that in class2 with this way but don't work:

class1.h

extern int saeid;   // this is global variable

@interface class1 : UITableViewController<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) IBOutlet UITableView *table;

@end

class1.m

#import "class1.h"
#import "class2.h"

@implementation class1
{
    int saeid;
}
@synthesize table;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
    int x = (indexPath.row)+1;
    saeid = x;                      //initialize global variable 
    NSLog(@"X & SAEID: %d & %d",x,saeid);
}

class2.h

#import "class1.h"

@interface class2 : UIViewController<UIScrollViewDelegate>
{    
}

@end

class2.m

#import "class2.h"

@implementation class2
{
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"Saeid in class2 : %d",saeid);

}

2 Answers 2

1

There seems to be some confusion here. Most importantly, a global variable cannot be "in" a class--global variables are by definition outside of any classes. So if you really want a global variable (more on this later) then you need to take the int saeid; definition in class1.m outside of the class definition, and just have it at the file level.

After you've done that, things still won't compile. The statement extern int saeid; roughly says to the compiler "I've defined an integer named saeid somewhere else, so just pretend it exists and let the linker figure out how to hook it up." There is no reason to have this statement in class1.h because this global variable is not used anywhere in that file. Instead, you should put this extern statement near the top of class2.m. It is used in that file, so you need to assure the compiler that the variable is defined somewhere as it is compiling that file.

Those steps should get your code to compile. But now you should stop and think about whether or not you really want a global variable. Global variables tie your classes together and make it hard to change one without affecting (and possibly breaking) others. They make it harder to test your code, and they make it more confusing to read your code. Another option to consider here is to create saeid as a property on the class1 class, and add a class1* property to class2. Then when you create your class2 instance, pass along a pointer to the existing class1 instance. The class2 instance can keep that pointer and use it to access the saeid property as needed.

Sign up to request clarification or add additional context in comments.

1 Comment

my friend can you more explain ? with code I'm not good in English.
0

In Objectice-C you can't have class variables, just instance variables.

If you want to have a global var you'd write:

#import "class1.h"
#import "class2.h"

int saeid;

@implementation class1
{
}
@synthesize table;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
    int x = (indexPath.row)+1;
    saeid = x;                      //initialize global variable 
    NSLog(@"X & SAEID: %d & %d",x,saeid);
}

But that's just a global var, it's got nothing to do with the class!

2 Comments

I want get value that global variable in class1 and use that value in class2
Your code does not show the allocation of the classes or the sequence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.