[問題] 無法解釋的簡單類別問題
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux : gcc 版本 4.1.2 20070925 (Red Hat 4.1.2-33)
問題(Question):
以下的類別程式不曉得如何解釋,請大家提供看法。
預期的正確結果(Expected Output):
據我的認知,兩個輸出式應該都輸出 0 ,但
第一個輸出在編譯時就會有問題,以下為編譯錯誤訊息:
error: request for member 'fval' in 'a', which is of non-class type 'AA ()(BB
(*)())'
程式碼(Code):(請善用置底文網頁, 記得排版)
class BB {
private :
int y ;
public :
BB( int s = 0 ) : y(s) {}
double val() const { return y ; }
};
class AA {
private :
BB fn ;
public :
AA( const BB& h ) : fn(h) { }
double fval() const { return fn.val() ; }
};
int main() {
AA a(BB()) ;
cout << a.fval() << endl ; // 此行編譯錯誤
AA b(BB(0));
cout << b.fval() << endl ;
return 0 ;
}
--
※ 編輯: pziyout 來自: 140.115.25.21 (03/06 08:15)
→
03/06 08:21, , 1F
03/06 08:21, 1F
→
03/06 09:01, , 2F
03/06 09:01, 2F
→
03/06 09:42, , 3F
03/06 09:42, 3F
→
03/06 09:55, , 4F
03/06 09:55, 4F
→
03/06 09:55, , 5F
03/06 09:55, 5F
推
03/06 10:17, , 6F
03/06 10:17, 6F
→
03/06 10:20, , 7F
03/06 10:20, 7F
→
03/06 10:21, , 8F
03/06 10:21, 8F
→
03/06 10:21, , 9F
03/06 10:21, 9F
→
03/06 10:29, , 10F
03/06 10:29, 10F
→
03/06 10:54, , 11F
03/06 10:54, 11F
推
03/06 11:20, , 12F
03/06 11:20, 12F
→
03/06 13:25, , 13F
03/06 13:25, 13F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章