2013-04-11 159 views
1

中將編譯源設置爲Objective-C++之後,我創建了一個新的單個視圖項目來測試它。在我的ViewController.m裏面是代碼:字符數組的初始化字符串太長。在Xcode

我不知道爲什麼當我將我的編譯源設置爲ObjectiveC++時,它給了我這個錯誤? 初始化串爲字符數組太長

static const char _basex[3] = "12"; <-This is always ok 
static const char _basex2[2] = "12"; <-Gives the initializer error when compiler set to Objective-C++ 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

回答

4

的C-字符串文字"12"需要3個字符,12,和空終止。

如果要初始化2 char數組,這樣做:

靜態常量字符_basex2 [2] = { '1', '2'};

+0

那麼當編譯源設置爲「根據文件類型」時,編譯器怎麼會不給我一個錯誤? – mskw 2013-04-11 16:20:27

+0

@mskw - 什麼是文件後綴? – 2013-04-11 16:22:00

+0

後綴是.m – mskw 2013-04-11 16:22:31