I am studying Lafore's 4th editing of C++ book and I am stuck with this problem.
I have these two classes, CountDn derives from Counter. In CountDn I want to overload the prefix for decrement operator and the postfix for both increment and decrement.
It works with all the operators except when I try to do ++c11
.
I get these errors from the compiler:
50:10: error: no match for 'operator++' (operand type is 'CountDn')
50:10: note: candidate is:
41:13: note: CountDn
CountDn::operator++(int)
41:13: note: candidate expects 1 argument, 0 provided
Even though get_count()
works fine I do not understand why the prefix operator does not work.
My thoughts here is if CounterDn class derives from Counter all the functions that are public should be accessible. What can I revise so I can understand the solution for this problem better?
#include <iostream>
using namespace std;
class Counter{
protected:
unsigned int count; //count
public:
Counter() : count(0) //constructor, no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() const //return count
{
return count;
}
Counter operator ++ () //incr count (prefix)
{
return Counter(++count);
}
};
class CountDn : public Counter{
public:
CountDn() : Counter() //constructor, no args
{ }
CountDn(int c): Counter(c) //constructor, 1 arg
{ }
CountDn operator -- () //decr count (prefix)
{
return CountDn(--count);
}
CountDn operator --(int){
return CountDn(count --);
}
CountDn operator ++(int)
{
return CountDn(count++);
}
};
int main() {
CountDn c1(10),c2;
c2 = ++c1;
cout << c1.get_count() << endl;
return 0;
}