[問題] 主程式include各class.h的問題

看板C_and_CPP作者 (妤欣)時間4年前 (2019/07/22 15:53), 4年前編輯推噓5(505)
留言10則, 7人參與, 4年前最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) vs 2017 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) vc++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) stdlib 問題(Question): 1. 我有一個主程式, 以 main() 為入口點 因為這個主程式 main 會用到其他 class A, B, C main 本身要 include A, B, C .h 也要 include <iostream> ... 等等 但是 A, B, C.h 裡面也有 include 過 <iostream> 了 這樣會有問題嗎? 該如何避免這個問題呢 2. main 中需要 #define Length 透過編譯時給定參數 ex. @ main.cpp #ifndefine Length #define Length 100 #endif A, B, C.h 也都有上面這段 所以main include A.h, B.h, C.h 之後就重複了 請問有辦法避開嗎~ 謝謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.137.108.25 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1563782037.A.0AC.html

07/22 16:26, 4年前 , 1F
google: include guard
07/22 16:26, 1F
謝謝提供關鍵字

07/22 16:46, 4年前 , 2F
你已經得到它了! #ifndefine #define #endifdefine
07/22 16:46, 2F

07/22 16:47, 4年前 , 3F
#endif
07/22 16:47, 3F
謝謝告知

07/22 17:54, 4年前 , 4F
上次的 lex 後來有順利解決嗎?
07/22 17:54, 4F
有喔,謝謝你~ ※ 編輯: OfficeGL (114.137.108.25 臺灣), 07/22/2019 18:23:03 ※ 編輯: OfficeGL (114.137.108.25 臺灣), 07/22/2019 18:23:29

07/22 21:03, 4年前 , 5F
#pragma once ?
07/22 21:03, 5F

07/22 22:07, 4年前 , 6F
樓上的做法不好, 可以谷歌為什麼
07/22 22:07, 6F

07/23 08:34, 4年前 , 7F
個人以為 #pragma once 的缺點只有它是 de facto 標準
07/23 08:34, 7F

07/23 08:35, 4年前 , 8F
(畢竟它本質上是 #pragma, 不會也不可能成為正式標準)
07/23 08:35, 8F

07/23 08:38, 4年前 , 9F
其他方面來說我會覺得使用方便性上和語意上都比正式做法好
07/23 08:38, 9F

07/23 18:54, 4年前 , 10F
同意樓上
07/23 18:54, 10F
文章代碼(AID): #1TDMkL2i (C_and_CPP)