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

作者: OfficeGL (努力上班族)   2019-07-22 15:53:55
開發平台(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 之後就重複了
請問有辦法避開嗎~
謝謝~
作者: BlazarArc (Midnight Sun)   2019-07-23 18:54:00
同意樓上
作者: james732 (好人超)   2019-07-22 16:26:00
google: include guard
作者: Gway (我愛的你 也愛我 好嗎?)   2019-07-22 16:46:00
你已經得到它了! #ifndefine #define #endifdefine#endif
作者: Schottky (順風相送)   2019-07-22 17:54:00
上次的 lex 後來有順利解決嗎?
作者: s4300026 (s4300026)   2019-07-22 21:03:00
#pragma once ?
作者: loveme00835 (髮箍)   2019-07-22 22:07:00
樓上的做法不好, 可以谷歌為什麼
作者: LPH66 (-6.2598534e+18f)   2019-07-23 08:34:00
個人以為 #pragma once 的缺點只有它是 de facto 標準(畢竟它本質上是 #pragma, 不會也不可能成為正式標準)其他方面來說我會覺得使用方便性上和語意上都比正式做法好

Links booklink

Contact Us: admin [ a t ] ucptt.com