Re: [問題] class的使用以及建構子的問題

看板C_and_CPP作者 (我是殺菌)時間16年前 (2009/12/27 16:57), 編輯推噓1(102)
留言3則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《liu2007 (薯)》之銘言: : 我在define.h 檔案中定義了自己的 兩個struct : 編譯之後的錯誤訊息 : error C2011: 'node' : 'struct' type redefinition : error C2011: 'Pnode' : 'struct' type redefinition : 我應該是沒有重複定義啊..... : 第二個問題是: : 可是我在class的.cpp檔裡面打 class_name:: : 出現的表單卻沒有建構子的這個item : 一開始我以為是跟回傳值有關 : 但解構子卻有有在表單裡面 這兩個問題,都是忘記在 .h 加入 #ifndef 和 #endif 的原因。 讓編譯器重複讀取 define.h 和不知道 .h 已經結尾。 其實編譯器幫你載入了兩次 define.h,只是你沒有發現。 第一次是在 include.h, 而第二次是在引入 adrsQueue.h 又載入一次 define.h 正確的解法是 : 告訴編譯器說只要載入一次 define.h #ifndef __TEST_H__ // __TEST_H__ 由 progrmmer 自己取名 #define __TEST_H__ //----------------------------------------------------------------- struct node { char data; struct node *left_child, *right_child; }; typedef node dataNode; //----------------------------------------------------------------- struct Pnode { dataNode *point; struct Pnode *next; }; typedef Pnode adrsNode; //----------------------------------------------------------------- #endif -- ※ 發信站: 批踢踢實業坊(ptt.cc) ※ 編輯: msc0953 來自: 219.87.141.226 (12/27 17:00)

12/27 17:07, , 1F
真的耶,而且如果把include.h 的#include "define.h"
12/27 17:07, 1F

12/27 17:07, , 2F
// 掉 他反而會說沒有定義,感謝原PO解答
12/27 17:07, 2F

12/27 17:08, , 3F
之前看到#ifndef #endif 都不知道幹麻,原來是這個功用^^
12/27 17:08, 3F
文章代碼(AID): #1BDo62ig (C_and_CPP)
文章代碼(AID): #1BDo62ig (C_and_CPP)