error

Guys, can u plz help me out. Why is following error coming twice in the folowing program?
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class counter' (or there is no acceptable conversion)



#include<iostream>

using namespace std;

class counter
{
protected:
int count;
public:

counter():count(0)
{}

counter(int c): count(c)
{}

unsigned int get_count()
{
return count;
}

counter operator++()
{
count++;
return counter(count);
}

counter operator++ (int)
{
return counter(count++);
}


};


class countDn : public counter
{
public:
counter operator-- ()
{
return counter(--count);
}

counter operator-- (int)
{
return counter(count--);
}

};


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

++c1;
c2 = ++c1;

cout<<"Counter in c1: "<<c1.get_count()<<endl;
cout<<"Counter in c2: "<<c2.get_count()<<endl;

c2 = c1++;

cout<<"Counter in c1: "<<c1.get_count()<<endl;
cout<<"Counter in c2: "<<c2.get_count()<<endl;

c1--;
c2--;

cout<<"Counter in c1: "<<c1.get_count()<<endl;
cout<<"Counter in c2: "<<c2.get_count()<<endl;
return 0;
}
I think it more to do with this particular statement.
c2 = ++c1;

Now both c2 and c1 are of class countDn

when the compiler does operation ++c1, the ++ (pre-increment) operator of the counter base class is called, because the countDn class does not provide a ++ operator of it's own.
We note that the counter class ++ function counter operator++() returns a counter
object.

So this statement c2 = ++c1 will end up trying to assign a counter object to a countDn object.
As we have NOT made an assignment operator function in the countDn class to allow
for this to happen - then we get the error reported by the compiler.



The same thing applies to this c2 = c1++; statement
Last edited on
Hey Thanks Gloucestor !! I have understood.
Topic archived. No new replies allowed.