編譯錯誤--------:XX does not name a type和field `XX' has incomplete type
</pre><pre name="code" class="html"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">兩個C++編譯錯誤及解決辦法--does not name a type和field `XX' has incomplete type</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"> </p><table border="0" style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><tbody><tr><td><p>
</p><p>編譯錯誤一:XX does not name a type</p><p>編譯錯誤二:field `XX' has incomplete type</p><p>編譯錯誤一:XX does not name a type</p><p>例子如下(在別處看到的):</p><p>class B{</p><p>public:</p><p>B(){}</p><p>~B(){}</p><p>private:</p><p>A a;</p><p>};</p><p>class A{</p><p>public:</p><p>A(){}</p><p>~A(){}</p><p>private:</p><p>int a;</p><p>};</p><p>編譯成則將報一個error:"A does not name a type"</p><p>報錯的位置為紅色那一行。</p><p>即使clase A和class B分別在兩個檔案定義,並且在定義B的檔案頭中#include了class A的標頭檔案也同樣會報這個錯(這是因為編譯和連結之間的先後關係造成的)。</p><p>解決該錯誤的辦法:</p><p>在class B定義宣告之前先宣告一下class A, 如下:</p><p>class A;</p><p>class B{</p><p>public:</p><p>B(){}</p><p>~B(){}</p><p>private:</p><p>A a;</p><p>};</p><p>class A{</p><p>public:</p><p>A(){}</p><p>~A(){}</p><p>private:</p><p>int a;</p><p>};</p><p>編譯錯誤二:field `XX' has incomplete type</p><p>同樣緊跟上面的例子,通過問題一的辦法,第一個錯誤已經消失,但是馬上第二個錯誤就出現了!還是同一個位置。</p><p>這個錯誤的意思,就是說class B中的XX域的型別不夠完整,為什麼呢?明明class A後面已經定義好了啊。其實原因還是和前面一樣,在class B定義之前,我們也只是對class A進行了宣告而並沒有進行具體的定義,所以解決該錯誤的方法是:</p><p>將class B定義中的A域改用指標就行了。改正後程式碼為:</p><p>class A;</p><p>class B{</p><p>public:</p><p>B(){}</p><p>~B(){}</p><p>private:</p><p>A *a;</p><p>};</p><p>class A{</p><p>public:</p><p>A(){}</p><p>~A(){}</p><p>private:</p><p>int a;</p><p>};</p><p>這樣,這段簡單的程式碼才能沒有錯誤的通過編譯。</p></td></tr></tbody></table>
相關文章
- 編譯錯誤 --- does not name a type和field `XX' has incomplete type編譯
- 兩個C++編譯錯誤及解決辦法--does not name a type和field `XX' has incomplete typeC++編譯
- QT中error: xxx does not name a type xxx錯誤QTError
- 編譯錯誤導致浪費10多分鐘, 編譯錯誤的提示:xxx does not name a type xxx編譯
- error: ‘xxx’ does not name a typeError
- ndk編譯錯誤:error: 'override' does not name a type編譯ErrorIDE
- eclipse :報錯 ‘XXXX‘ does not name a type的解決辦法Eclipse
- PostgreSQL cache lookup failed for type XXXX 錯誤SQLAI
- Qt5.7中報錯“xxx dose not name a type”的原因;QT
- `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
- xxx cannot be resolved to a type
- Array type xxx is not assignable
- error: 'cout' does not name a type|Error
- does not name a type 的可能性
- error: ‘變數名‘ does not name a type|Error變數
- 'int32_t' does not name a type
- beego報錯 table name: `xxx` not existsGo
- xcode6編譯錯誤,提示Expected a typeXCode編譯
- Qt 標頭檔案互相包含會報錯'' does not name a typeQT
- The type XXX cannot be resolved.It is indirectly referenced from required .classUI
- flutter和Android混編下出現“No implementation found for method xxxx on channel”錯誤FlutterAndroid
- a commponent required a bean of type XXXXXX that could not be found-2022新專案UIBean
- Checklist for PRVF-10037 : Failed to retrieve storage type for xx on node xxAI
- Linux make編譯報錯:undefined reference to `vtable for XXX 'Linux編譯Undefined
- 更新xcode8後真機除錯報code signing is required for product type 'xxxxx' in SDK 'iOS 10.0'XCode除錯UIiOS
- Spring:錯誤No bean named 'xxxxxxx' availableSpringBeanAI
- dcat-admin 表單 Field type [autocomplete] does not exist.
- Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa(解決的兩種方式)Go
- error:dereferencing pointer to incomplete typeError
- 關於報錯Error creating bean with name 'xxxxx': Invocation of init method....ErrorBean
- value xxx for the flexfield segment customer:does not exist in the value set xxxxFlex
- Git錯誤fatal: CRLF would be replaced by LF in xxxGit
- linux解決“XXX is not in the sudoers file”錯誤Linux