作者ric2k1 (Ric)
看板EE_DSnP
標題[情報] make test 的 error
時間Thu Nov 4 23:19:35 2010
連續兩天有同學遇到這樣的 compile error 來問我,
我想一定還會有人遇到,就統一在這裡回答。
================================================
> compiling: test.cpp
In file included from test.cpp:10:
../../include/calcModNum.h: In static member function ‘static bool
ModNum::ge\
tStrVal(const std::string&, ModNum&)’:
../../include/calcModNum.h:101: error: ‘isValidVarName’ was not declared in
\
this scope
../../include/calcModNum.h:103: error: ‘myStr2Int’ was not declared in this
\
scope
make[1]: *** [test.o] Error 1
make: *** [testmain] Error 2
================================================
兩個人造成的原因一樣,就是在 calcModNum.h 裏頭呼叫了 "isValidVarName()" 以及
"myStr2Int()" 兩個 functions.
因為這兩個 functions 是定義在 myString.cpp 裏頭的,
如果你在 calcModNum.h 沒有宣告這兩個 function 的 prototypes,
compiler 就不會知道他們是甚麼東西。
解決的方式:
1. 在 calcModNum.h 裏頭 #include "util.h"
但這樣並不好,我不太鼓勵在 .h 裏頭隨便 include 其他的 .h 檔
2. 加上:
extern int myStrNCmp(const string& s1, const string& s2, unsigned n);
extern bool myStr2Int(const string& str, int& num);
勉強可以接受,因為我也不太喜歡在 .h 裏頭看到太多 extern 甚麼的...
(個人觀感啦)
3. 將使用這兩個 functions 的 member function(s) 定義到 calcModNum.cpp
推薦,這樣比較乾淨。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 123.193.11.13