編譯器報錯提示 此聲明沒有存儲類或類型說明符
或 xx does not name a type
個人原因
因為我在頭文件中運行了如下語句
struct EXAMPLE examples;
examples.input = "hello world"
但是 函數外只能定義全局變量或者對象 ,而不能執行語句及調用函數 。
可以改為
struct EXAMPLE examples = {.input = "hello world"};
但是注意C語言中結構體初始化時,對於內部元素的順序沒有要求,但是C++不一樣。
因為C++結構體初始化時,必須按照定義的順序進行初始化,不能夠跳過其中內容而初始化其他選項,或者定義的順序先后有問題。
否則會報錯:sorry, unimplemented: non-trivial designated initializers not supported
c++最好這么寫
struct EXAMPLE examples = {"hello world"};
原因2
i just had this problem, and like Arckaroph have said : the problem is that when we include
a header file in a source code file, and we use in it the directive #ifndef, we can't include
it again in a header file to give a type of it's included class to a variable in source code file
example :
class1.h contains Class1 class2.h contains Class2 class2 have a private variable V with
class1 type if we include class1.h in class2.CPP we can't include it in class2.h to give V a class1 type.
so we put in class2.cpp class2.h before class1.h or we delete class1.h from class2.cpp