您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

解決編譯錯誤:typedef redefinition with different types

編輯:iOS 開發綜合

在 iOS 專案中,如果同一個自定義型別(typedef)在多個檔案裡被重複定義,編譯時就可能出現「typedef redefinition with different types」的錯誤。本文記錄這個問題的成因與解法。

一、問題重現

假設有兩個檔案都定義了同名的 ConnectionType 列舉。第一個檔案:

#import <Foundation/Foundation.h>
#import "Blog.h"

// Types
typedef enum {
    ConnectionTypeAsynchronously,
    ConnectionTypeSynchronously
} ConnectionType;

@class Parser;

@protocol ParserDelegate <NSObject>
end

第二個檔案:

#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"

typedef enum {
    DownloaderTypeUnknown,
    DownloaderTypeData,
    DownloaderTypeImage,
    DownloaderTypeAudio,
    DownloaderTypeVideo,
    DownloaderTypeFile
} DownloaderType;

// Types
typedef enum {
    ConnectionTypeAsynchronously,
    ConnectionTypeSynchronously
} ConnectionType;

@class LRDHttpRequest;

@protocol LRDHttpRequestDelegate <NSObject>
end

錯誤訊息如下:

error: typedef redefinition with different types ('ConnectionType' vs 'ConnectionType')

二、原因與解法

問題的核心是「衝突、重複」:同一個 ConnectionType 型別被定義了兩次。最簡單的解法就是刪除其中一處重複的定義即可。

更根本的做法,是把這些共用型別抽到一个獨立的標頭檔中集中定義,再透過 #import 或 #ifndef guards 避免重複引入,這樣就能從源頭杜絕這類 typedef 重複定義的錯誤。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved