2015-09-18 37 views
1

這是代碼。我在C++中的代碼有什麼問題?

#include <iostream> 
#include <cstring> 
using namespace std; 
class Headquarter 
{ 
private: 
    //num of total warriors 
    int totalNum; 
public: 
int getTotalNum() ; 

}; 
int Headquarter::getTotalNum() 
{ return totalNum; } 
int main() 
{ 
    Headquarter a; 
    Headquarter *p =&a; 
    cout << (p->getTotalNum) << endl; 
    return 0; 
} 

我無法用g ++,4.8.4編譯它。我不知道它有什麼問題。 這是錯誤消息:

test.cpp: In function ‘int main()’: 
test.cpp:19:7: error: no match for ‘operator<<’ (operand types are        std::ostream {aka std::basic_ostream<char>}’ and ‘<unresolved>`overloaded function type>’) 

COUT < <(對 - > getTotalNum)< < ENDL; ^

回答

8
cout << (p->getTotalNum) << endl; 

getTotalNum是一個類方法。因此它必須像任何其他功能一樣被調用:

cout << (p->getTotalNum()) << endl; 
+0

謝謝!你真的幫了我很多! –