[問題] 類別分.h.cpp分開寫後碰到的問題

看板C_and_CPP (C/C++)作者 (更獨立)時間15年前 (2010/09/08 13:25), 編輯推噓0(007)
留言7則, 4人參與, 最新討論串1/1
小的是C++新手,一直沒有把類別用.h和.cpp分開寫的習慣 今天小試了一下後碰到了一個問題 /* main.cpp*/ #include "common.h" #include "calculator.h" int main(int argc, char *argv[]) { calculator cook; cook.add(2); cook.sub(2); cout<<1<<endl; system("PAUSE"); return EXIT_SUCCESS; } /* common.h*/ #ifndef common_h #define common_h #include <iostream> #include <cstdlib> #include <ctime> using namespace std; #endif /* calculator.h */ #ifndef calculator_h #define calculator_h class calculator{ private: protected: public: void add(int data); void sub(int data); }; #endif /* calculator.cpp */ #include "common.h" #include "calculator.h" calculator::calculator(){cout<<"welcome"<<endl;} void calculator::add(int data){cout<<data<<endl;} void calculator::sub(int data){cout<<data+1<<endl;} ________________________________________________________________ 假如calculator.cpp沒有建構者函式的話可以順利編譯 但是有建構者函式卻不能順利編譯,所以不知道問題出在哪 可以請過來人解答一下嗎,謝謝 ________________________________________________________________ 環境 devC++ 原因 3 C:\Documents and Settings\user\桌面\新資料夾\calculator.cpp definition of implicitly-declared `calculator::calculator()' 3 C:\Documents and Settings\user\桌面\新資料夾\calculator.cpp declaration of `calculator::calculator()' throws different exceptions 3 C:\Documents and Settings\user\桌面\新資料夾\calculator.h than previous declaration `calculator::calculator() throw ()' C:\Documents and Settings\user\桌面\新資料夾\Makefile.win [Build Error] [calculator.o] Error 1 不好意思漏掉了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.47.229.212

09/08 13:26, , 1F
請貼上你的編譯環境與錯誤訊息
09/08 13:26, 1F
※ 編輯: yahooc 來自: 114.47.229.212 (09/08 13:27)

09/08 13:27, , 2F
declare your constructor in the header.
09/08 13:27, 2F

09/08 13:30, , 3F
(二樓)不懂 可以說明一下嗎
09/08 13:30, 3F

09/08 13:33, , 4F
class calculator { public: calculator(); }; 要有這個
09/08 13:33, 4F

09/08 13:35, , 5F
謝謝
09/08 13:35, 5F

09/08 13:38, , 6F
header裡不要在global用usging..前幾篇就有寫原因XD
09/08 13:38, 6F

09/08 13:39, , 7F
好的 我會多爬文的 不勝感激
09/08 13:39, 7F
文章代碼(AID): #1CXnutrZ (C_and_CPP)
文章代碼(AID): #1CXnutrZ (C_and_CPP)