【问题标题】:Duplicate Symbol error xcode iphone重复符号错误xcode iphone
【发布时间】:2012-03-19 23:03:25
【问题描述】:

在构建和运行时,我的 xcode 中出现重复错误

我有两个文件 file1.m 和 file2.m 都使用相同的变量和函数名

file1.h

#import <UIKit/UIKit.h>


@interface file1 : UIViewController {

IBOutlet UILabel *result;   

}

-(IBAction)home;

@end

file1.m

#include<file1.h>
@implementation file1
int count = 0;
int arr[2][2];

file2.h

#import <UIKit/UIKit.h>


@interface file2 : UIViewController {

IBOutlet UILabel *result;   

}

-(IBAction)home;

@end

file2.m

#include<file2.h>
@implementation file2
int count = 0;
int arr[2][2];

当构建并运行时,它在 file1.o 和 file2 中给出错误重复符号“count”。 ○ 如果我将他们的名字改为 count1 和 count2,我不会收到任何错误。

在 file1.m 和 file2.m 中,我都在尝试创建全局变量。

有什么方法可以在两个文件中使用相同名称的变量和函数

【问题讨论】:

  • 你也可以发布 .h 吗?
  • 你在这里定义全局变量吗?如果是这样,你打算这样做吗?请提供更多代码。
  • 我发布了一个新代码。 @HermannKlecker 是的,我正在尝试定义全局变量

标签: iphone objective-c xcode


【解决方案1】:

让他们static:

static int count = 0;
static int arr[2][2];

请注意,它们将引用不同的变量。如果您希望他们引用 same 变量,请将其保留在一个文件中并在另一个文件中声明它们 extern

extern int count;
extern int arr[2][2];

将这些 extern 声明放在一个公共标头中是很常见的。

【讨论】:

  • file1.m 变量和 file2.m 变量没有任何关系我希望它们分开
  • 然后static 是要走的路。
  • 如果我在两个 .m 文件中有相同的函数名怎么办
  • 好吧,既然您似乎在谈论的“函数”实际上是实例方法,那将不是问题。不过,就像我的示例一样,您可以拥有两个同名的 static 函数。这有点奇怪,但众所周知。
  • 在 file1.m 中我使用了 int count = 0;并在 file2.m 中静态 int count = 0;谢谢它的工作
猜你喜欢
  • 2012-12-31
  • 1970-01-01
  • 2012-10-23
  • 2014-09-03
  • 2015-06-20
  • 1970-01-01
  • 2015-06-18
  • 2016-02-03
  • 2011-03-23
相关资源
最近更新 更多