CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Postfix Operator Overloading Error!

    Gretting Codeguru World!
    Recently, i successfully overloaded postfix operator to class counter by using Object and Class. Now, i want to overload same postfix operator to Inheritance. I created my code and generated in compiler. But, my compiler signaled me uncommon error(saw first time) and i couldn't generate any idea where my actually mistake is.

    Here is my Code which objective is to count the number increasingly or decreasingly as per object created of CountDn class.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Counter                                       // base class
    {
        protected :                                     // NOTE : Not Private
            unsigned int count;
        public :
            Counter() : count(0)                        // no arg constructor
            {}
            Counter(int c) : count(c)                   // one arg constructor
            {}
            unsigned int get_count() const              // return count
                {return count;}
    
            Counter operator ++()                       // increase count(postfix)
                {return Counter(count++);}
    
    };
    
    class CountDn : public Counter                      // derived class
    {
        public :
            CountDn() : Counter()
            {}
            CountDn(int c) : Counter(c)
            {}
            CountDn operator --()                       // decrease count(postfix)
                {return CountDn(count--);}
    };
    
    int main()
    {
        CountDn c1;
        CountDn c2;
        cout <<"\nC1 = "; c1.get_count();
        cout <<"\nC2 = "; c2.get_count();
        c1++;
        c2 = c1++;
        cout <<"\nC2 = "; c1.get_count();
        c2 = c1--;
        cout <<"\nC2 = "; c2.get_count();
        cout <<'\n';
        return 0;
    }
    Expecting some suggestion and Feedback

    Regards
    Basanta

    Error : |41|error: no 'operator++(int)' declared for postfix '++', trying prefix operator instead|
    |42|error: no 'operator++(int)' declared for postfix '++', trying prefix operator instead|
    |42|error: no match for 'operator=' in 'c2 = c1.CountDn::<anonymous>.Counter:perator++()'|
    |44|error: no 'operator--(int)' declared for postfix '--', trying prefix operator instead|

    Plus : I have visited http://forums.codeguru.com/showthrea...ment-operators but, i still can't understand the code so i have to create this thread..
    Last edited by basanta; July 27th, 2012 at 06:00 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured