Re: [問題] Link Error : Already Defined in main …

看板C_and_CPP (C/C++)作者 (我愛阿蓉)時間16年前 (2009/08/01 11:11), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《realmojo (YOYO)》之銘言: : === A.h === : #ifndef A_H : #define A_H : class A : { : static int a; : }; : int A::a = 5; ^^^^^^^^^^^^^^ 好習慣 .h只要寫宣告就好 而你這邊 寫這樣 就是定義 把這行擺到a.cpp 即可 會出現這error跟 ifndef有沒有寫無關 你從main.cpp 開始想 include a.h 就貼了一次A::a=5近來cpp了 然後A.cpp include a.h 又貼了一次 A::a=5近來 我不太會描述~ 不過這時雖然跑了兩次ifndef A_H兩次 但是這是不同的檔案去include這.h 所以ifndef A_H都是成立的 也就是對你這情況 這ifndef 無法避免 : #endif : ==A.cpp=== : #include "A.h" : ==main.cpp== : #include "A.h" : int main() : {} : 我用VC會出現Link error : "public: static int A::x" (?x@A@@2HA) already defined in main.obj" : 問題出在A.cpp也有#include A.h,我想問為什麼已經有宣告#ifndef了,還會重復進入呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.207.187
文章代碼(AID): #1ASx97gz (C_and_CPP)
文章代碼(AID): #1ASx97gz (C_and_CPP)