我的 c++ 代码有什么问题?

What's wrong with my code in c++?

本文关键字:问题 什么 c++ 代码 我的      更新时间:2023-10-16

这是代码。

#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 & lt; & lt;(p -> getTotalNum) & lt; & lt;endl;^

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

getTotalNum是一个类方法。因此,它必须像其他函数一样被调用:

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