編譯錯誤 --- does not name a type和field `XX' has incomplete type
兩個C++編譯錯誤及解決辦法--does not name a type和field `XX' has incomplete type
編譯錯誤一:XX does not name a type 編譯錯誤二:field `XX' has incomplete type 編譯錯誤一:XX does not name a type, 中文意思為“XX沒有命名一個型別“ 拿個例子來講,如果兩個類定義如下: class B{ public: B(){} ~B(){} private: A a; }; class A{ public: A(){} ~A(){} private: int a; }; 編譯成則將報一個error:"A does not name a type" 報錯的位置為紅色那一行。 即使clase A和class B分別在兩個檔案定義,並且在定義B的檔案頭中#include了class A的標頭檔案也同樣會報這個錯(這是因為編譯和連結之間的先後關係造成的)。 解決該錯誤的辦法: 在class B定義宣告之前先宣告一下class A, 如下: class A; class B{ public: B(){} ~B(){} private: A a; }; class A{ public: A(){} ~A(){} private: int a; }; 編譯錯誤二:field `XX' has incomplete type 同樣緊跟上面的例子,通過問題一的辦法,第一個錯誤已經消失,但是馬上第二個錯誤就出現了!還是同一個位置。 這個錯誤的意思,就是說class B中的XX域的型別不夠完整,為什麼呢?明明class A後面已經定義好了啊。其實原因還是和前面一樣,在class B定義之前,我們也只是對class A進行了宣告而並沒有進行具體的定義,所以解決該錯誤的方法是: 將class B定義中的A域改用指標就行了。改正後程式碼為: class A; class B{ public: B(){} ~B(){} private: A *a; }; class A{ public: A(){} ~A(){} private: int a; }; 這樣,這段簡單的程式碼才能沒有錯誤的通過編譯。 |
相關文章
- 編譯錯誤--------:XX does not name a type和field `XX' has incomplete type編譯
- 兩個C++編譯錯誤及解決辦法--does not name a type和field `XX' has incomplete typeC++編譯
- ndk編譯錯誤:error: 'override' does not name a type編譯ErrorIDE
- 編譯錯誤導致浪費10多分鐘, 編譯錯誤的提示:xxx does not name a type xxx編譯
- QT中error: xxx does not name a type xxx錯誤QTError
- error: ‘xxx’ does not name a typeError
- `QtValidLicenseForCoreModule' does not name a type 錯誤的解決QTREM
- ‘map’ does not name a type
- vector does not name a type
- error :does not name a typeError
- error: '[class name]' does not name a typeError
- Qt 報錯 “類名”does not name a typeQT
- error: 'cout' does not name a type|Error
- eclipse :報錯 ‘XXXX‘ does not name a type的解決辦法Eclipse
- does not name a type 的可能性
- error: ‘變數名‘ does not name a type|Error變數
- 'int32_t' does not name a type
- Qt5.7中報錯“xxx dose not name a type”的原因;QT
- xcode6編譯錯誤,提示Expected a typeXCode編譯
- PostgreSQL cache lookup failed for type XXXX 錯誤SQLAI
- Qt 標頭檔案互相包含會報錯'' does not name a typeQT
- dcat-admin 表單 Field type [autocomplete] does not exist.
- error:dereferencing pointer to incomplete typeError
- Android之NDK開發錯誤 error: unknown type name 'JNIEXPORT'AndroidErrorExport
- A resource type with the name 'ora.daemon.type' is already registered
- Eclipse中執行出現selection does not contain a main type錯誤EclipseAI
- xxx cannot be resolved to a type
- Array type xxx is not assignable
- std::unique_ptr使用incomplete type的報錯分析和解決
- 通俗易懂解釋一類和二類錯誤(Type I Error Type II Error)Error
- C++ doesn't name a typeC++
- PSQLexception: ERROR : type "signed" does not existSQLExceptionError
- editor does not contain a main type Maven專案AIMaven
- MYSQL中 TYPE=MyISAM 錯誤的解決方法MySql
- 實體類註解錯誤:Could not determine type for:
- SAP MRKO Error - For document type RE, an entry is required in field Reference -ErrorUI
- 如何處理ABAP DDIC_TYPE_INCONSISTENCY錯誤
- vue Cannot find module @/xxx/xxx.ts or its corresponding typeVue