從網上找到了以下幾點 https://blog.csdn.net/fly20180712/article/details/88306008
1、沒有加調用函數的頭文件
2、不存在xxx命名空間
3、包含頭文件,但是調用的時候,類名寫錯了
但是我睜大了眼睛也沒看到錯誤在哪里,直到后來注意到我似乎出現了循環定義。
有兩個文件,一個類 一個結構體結構如下
log.h
#include "Squeue.h"
struct event_info{
int fd;
Squeue q;
};
Squeue.h
...
#include "log.h"
class Squeue{
event_info* ei;
public:
int push(char* src,int len){
...
ei->q->push(...);
...
}
};
...
上面出現的問題就是,event_info
結構體中用到了Squeue
類,但是同時Squeue
也用到了event_info
結構體
這樣就會出現環形定義的情況,會出現does not name a type
的錯誤。
但是根據提示根本不知道問題是什么,直到我注意到了環形定義。然后在結構體定義的上面寫上了class Squeue;
向前聲明。然后錯誤變化了,變成了"field has incomplete type"
。
通過查詢這個錯誤,找到了這個博客
https://blog.csdn.net/smcnjyddx0623/article/details/51736195
找到了解決方法,就是把上面結構體中的 Squeue q;
改成 Squeue *q;
當然,還需要修改對應的代碼,把 q.push()
修改成q->push()
原因是因為在定義結構體event_info
的時候,還沒有編譯到Squeue
,此時編譯器還不知道Squeue
的內部狀況。所以不能直接聲明本體,只能聲明指針。