C++ Builder 參考手冊 ? 常見的出錯和異常處理 ? Declaration of class TForm1 is missing or incorrect
- 出錯提示
- 產(chǎn)生原因及解決方法
1. 出錯提示
出錯時彈出對話框:“Error in module Unit1: Declaration of class TForm1 is missing or incorrect.”

出錯提示對話框
出錯時不能編譯,存盤也會彈出錯誤。

存盤時彈出的錯誤提示
2. 產(chǎn)生原因及解決方法
在 .h 文件里面的窗體類里面的 __published: 區(qū)段的前面含有大括號 { 和 } 引起的,雖然語法沒有錯誤,但是窗體設(shè)計找不到這個區(qū)段了。
例如,下面是一段出錯代碼:
class TForm1 : public TForm
{
public:
typedef struct { int a; } T;
__published: // IDE-managed Components
TMemo *Memo1;
TButton *Button1;
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};

在 __published: 之前出現(xiàn)大括號 { } 引起的錯誤
錯誤原因是在窗體類里面,在 __published: 之前出現(xiàn)大括號 { } 引起的錯誤,把 __published: 之前的代碼移到 __published: 之后,問題解決,修改之后的代碼:
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMemo *Memo1;
TButton *Button1;
public:
typedef struct { int a; } T;
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};

在 __published: 之后出現(xiàn)大括號 { } 不會出錯
另外一段代碼,也能正常編譯運行:
class TForm1 : public TForm
{
public:
typedef TNotifyEvent T;
__published: // IDE-managed Components
TMemo *Memo1;
TButton *Button1;
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};

在 __published: 之前沒有大括號,也不會出錯
問題總結(jié):
自己在窗體類里面添加的代碼,要添加在 __published: 代碼區(qū)段之后,因為 __published: 里面都是窗體編輯自動維護(hù)的內(nèi)容,__published: 之前的內(nèi)容可能會影響窗體編輯尋找 __published: 的方法,找不到了就會出錯,盡管語法都沒有錯誤,寫在后面就不會出錯。
相關(guān):
C++ Builder 參考手冊 ? 常見的出錯和異常處理 ? Declaration of class TForm1 is missing or incorrect