运算符重载错误无匹配运算符<<

时间:2019-10-18 10:07:40

标签: c++

我正在学习C ++中的运算符重载,我创建了一个程序来重载增量运算符后缀和前缀

显示错误,运算符不匹配<<< / p>

#include <iostream>

using namespace std;
class counter
{
    unsigned int countx;
public:
    counter(): countx(0)
    {}
    counter (int c) : countx(c)
    {}
    unsigned int get_count() const
    {
        return countx;
    }
    counter operator ++ ()
    {
        return counter(++countx);
    }
    counter operator ++ (int)
    {
        return counter(countx++);
    }
};

int main()
{
    counter c1, c2;
    cout<<endl<<"c1 : "<<c1.get_count;
    cout<<endl<<"c2 : "<<c2.get_count;

    ++c1;
    c2 = ++c1;
    cout<<endl<<"c1 : "<<c1.get_count;
    cout<<endl<<"c2 : "<<c1.get_count;

    c2= c1++;
    cout<<endl<<"c1 : "<<c1.get_count;
    cout<<endl<<"c2 : "<<c2.get_count;

    return 0;
}

请问您有什么建议可以解决此问题,并解释为什么会发生此错误?

1 个答案:

答案 0 :(得分:2)

我的编译器(llvm)说:

error: reference to non-static member function must be called; did you mean to call it with no arguments?
    cout<<endl<<"c1 : "<<c1.get_count;
                         ~~~^~~~~~~~~
                                     ()

建议您拨打电话:

cout<<endl<<"c1 : "<<c1.get_count();