ayaankhan98
ayaankhan98

Reputation: 5

operator overloading error no match operator <<

i am learning operator overloading in C++ i created a program to overload the increment operator both postfix and prefix

it shows error no match for operator<<

#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;
}

could you please suggest something to overcome this and also explain why this error occurs?

Upvotes: 0

Views: 71

Answers (1)

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36441

My compiler (llvm) says:

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;
                         ~~~^~~~~~~~~
                                     ()

that suggests you to make a call:

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

Upvotes: 2

Related Questions