-
July 27th, 2012, 04:27 AM
#1
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.
-
July 27th, 2012, 05:19 AM
#2
Re: Postfix Operator Overloading Error!
Originally Posted by basanta
But, my compiler signaled me uncommon error(saw first time) and i couldn't generate any idea where my actually mistake is.
Then you should have posted the error message too.
-
July 27th, 2012, 06:02 AM
#3
Re: Postfix Operator Overloading Error!
Originally Posted by laserlight
Then you should have posted the error message too.
Edited.
-
July 27th, 2012, 08:35 AM
#4
Re: Postfix Operator Overloading Error!
As the compiler states: you did not define a postfix operator++, although you comment your prefix operator++ as such.
Code:
// postfix: returns the object in its old state
Counter operator ++(int)
{ Counter temp(*this;) count++; return temp; }
// prefix: returns a reference to the object in its new state
Counter& operator ++()
{ count++; return *this; }
The third line in the error messages refers to the fact that you are trying to assign c1 to c2, but have not defined an assignment operator (Counter& operator=(const Counter& rhs)) in your class.
Last edited by Richard.J; July 27th, 2012 at 08:38 AM.
Reason: added comment
-
July 27th, 2012, 11:24 PM
#5
Re: Postfix Operator Overloading Error!
Originally Posted by Richard.J
As the compiler states: you did not define a postfix operator++, although you comment your prefix operator++ as such.
Code:
// postfix: returns the object in its old state
Counter operator ++(int)
{ Counter temp(*this;) count++; return temp; }
// prefix: returns a reference to the object in its new state
Counter& operator ++()
{ count++; return *this; }
The third line in the error messages refers to the fact that you are trying to assign c1 to c2, but have not defined an assignment operator (Counter& operator=(const Counter& rhs)) in your class.
Thnx Rechard Sir, With your suggestion, i rebuilded my code and it compiled well with (0 error and 0 warning) where at object i removed '=' sign and i just planned to increase c1 & c2 twice and again decrease c1 and c2 by once. But it didn't show desired output as i expected. It show blank atvalue.
Here is rebuilded Code
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 ++(int) // increase count(postfix)
{
return Counter(count++);
}
};
class CountDn : public Counter // derived class
{
public :
CountDn() : Counter()
{}
CountDn(int c) : Counter(c)
{}
CountDn operator --(int) // decrease count(postfix)
{return CountDn(count--);}
};
int main()
{
CountDn c1;
CountDn c2;
cout <<"\nC1 = "; c1.get_count();
cout <<"\nC2 = "; c2.get_count();
c1++;
c1++;
cout <<"\nAfter Increasing C1 Twice";
cout <<"\nC1 = "; c1.get_count();
c2++;
c2++;
cout <<"\nAfter Increasing C2 Twice";
cout <<"\nC2 = "; c2.get_count();
c1--;
cout <<"\nAfter Decreasing C1 Once";
cout <<"\nC1 = "; c1.get_count();
c2--;
cout <<"\nAfter Decreasing C2 Once";
cout <<"\nC2 = "; c2.get_count();
cout <<'\n';
return 0;
}
Plus : I initialized specific value of c1 and c2 at object to make sure it still not showing output, but, result appeared same as before.
Regards
Basanta
-
July 27th, 2012, 11:29 PM
#6
Re: Postfix Operator Overloading Error!
Originally Posted by basanta
Plus : I initialized specific value of c1 and c2 at object to make sure it still not showing output, but, result appeared same as before.
You should be debugging your code also. You can't just make changes, say it doesn't work, and not put the time into debugging your program.
Have you used your compiler's debugger? I would suggest you know how to use this tool, as you will be easily track the flow of your program, what functions are called, etc.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 27th, 2012 at 11:31 PM.
-
July 27th, 2012, 11:36 PM
#7
Re: Postfix Operator Overloading Error!
Originally Posted by basanta
Thnx Rechard Sir, With your suggestion, i rebuilded my code and it compiled well with (0 error and 0 warning) where at object i removed '=' sign and i just planned to increase c1 & c2 twice and again decrease c1 and c2 by once. But it didn't show desired output as i expected. It show blank atvalue.
Code:
cout <<"\nC1 = "; c1.get_count();
cout <<"\nC2 = "; c2.get_count();
Explain what these lines do. Next time, don't put multiple statements on one line.
Code:
cout <<"\nC1 = ";
c1.get_count();
cout <<"\nC2 = ";
c2.get_count();
Do you see the error now? The reason why you are not seeing any output is because you never outputted the values.
Regards,
Paul McKenzie
-
July 28th, 2012, 12:09 AM
#8
Re: Postfix Operator Overloading Error!
Originally Posted by Paul McKenzie
Code:
cout <<"\nC1 = ";
c1.get_count();
cout <<"\nC2 = ";
c2.get_count();
Do you see the error now? The reason why you are not seeing any output is because you never outputted the values.
Regards,
Paul McKenzie
Oh my God. I am so stupid. Actually Sir, I am learning C++ OOP just since 1 month ago so, i make some common errors.
Millions of thanks Paul Sir for signalling my error! Now, i got output as i expected.
Regards
Basanta
-
July 28th, 2012, 12:14 AM
#9
Re: Postfix Operator Overloading Error!
Originally Posted by Paul McKenzie
You should be debugging your code also. You can't just make changes, say it doesn't work, and not put the time into debugging your program.
Have you used your compiler's debugger? I would suggest you know how to use this tool, as you will be easily track the flow of your program, what functions are called, etc.
Regards,
Paul McKenzie
Sir, I am Using Code::Block v10.05. I actually don't know what debugging actually means. I simply write code and run it pressing 'F9' key. Can you give me some information about "how to debugg". Thanks in Advance
Regards
Basanta
-
July 28th, 2012, 02:56 AM
#10
Re: Postfix Operator Overloading Error!
Originally Posted by basanta
Sir, I am Using Code::Block v10.05.
Code::Blocks is not a compiler. It is an integrated development environment. The compiler that Code::Blocks uses is g++.
I actually don't know what debugging actually means.
Debugging is the process of fixing the problems with your program. It is a mandatory part of learning how to write programs. Programming isn't just about writing code, running it, and then posting on CodeGuru or some other board that the program doesn't work, waiting for someone to fix it for you.
You didn't really think that when we write programs 100 times bigger than yours, we write it with no bugs? No one writes error-free programs, and the process of fixing the programs is called debugging.
I simply write code and run it pressing 'F9' key.
Then you're not learning how to write programs. What happens when another program you write doesn't work? Are you going to post that program and have us do the job you're supposed to be doing? If you wrote the program, then it's a given you should know how to fix the program that you wrote yourself if something doesn't work.
Can you give me some information about "how to debugg". Thanks in Advance
Doesn't the Code::Blocks IDE give you instructions on how to use the debugger (which is actually based on gdb, the Gnu debugger)? A debugger allows you to single-step through your program, watch variables, set breakpoints, etc. I'm sure there must be a "Debug" menu option somewhere in CodeBlocks.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 28th, 2012 at 03:00 AM.
-
July 30th, 2012, 07:27 AM
#11
Re: Postfix Operator Overloading Error!
Originally Posted by basanta
Can you give me some information about "how to debug"
Debugging with Code::Blocks
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|