![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 14
| Code:
#include <iostream>
using namespace std;
class number //class to test the reprogramming of the "++" increment operator
{
public:
number();
~number();
int mynumber;
void inc(int increment) // replacement function for ++ operator
{
mynumber += increment;
}
number& operator ++ () // reprogramming the ++ operator
{
inc(1);
return *this;
}
void dmn() // dmn = display mynumber
{
cout << mynumber;
}
void setnum(int x)// sets mynumber value
{
mynumber = x;
}
};
int main()
{
int z;
cout << "Please choose start point : ";
cin >> z;
number newnumb();
newnumb.setnum(z);
newnumb.dmn();
++ newnumb;
newnumb.dmn();
system("PAUSE");
return 0;
}
1. error C2228: left of '.setnum' must have class/struct/union 2. error C2228: left of '.dmn' must have class/struct/union 3. error C2171: '++' : illegal on operands of type 'number (__cdecl *)(void)' 4. error C2105: '++' needs l-value 5. warning C4550: expression evaluates to a function which is missing an argument list 6. error C2228: left of '.dmn' must have class/struct/union 1, 2, and 6 annoy me, because I don't understand why it's printing it 3 and 4, I am just clueless as to what they mean... |
| Aaronugio is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You know what () means -- it means function call. This: Code: number newnumb; Code: number newnumb(); |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 14
| Thanks but that has lead me to another problem, when I changed it, it gave me these errors... : error LNK2019: unresolved external symbol "public: __thiscall number::~number(void)" (??1number@@QAE@XZ) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall number::number(void)" (??0number@@QAE@XZ) referenced in function _main do I need to manually call on the constructor and destructor? Last edited by Aaronugio; 07-20-2009 at 10:14 AM. |
| Aaronugio is offline | |
| | #4 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| You have declared a destructor, but there is no implementation. Code: ~number(); EDIT: Also, the code for your constructor is missing too. |
| bithub is offline | |
| | #5 | |
| The larch Join Date: May 2006
Posts: 3,082
| You have declared but not defined the constructor and destructor. (You can probably just remove the destructor declaration, and your constructor should probably set the number to some value - like 0, or do what setnum does.)
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #6 |
| Registered User Join Date: Jul 2009
Posts: 14
| Thanks Thanks you guys I'm sorry if this seemed very wasteful of your time, but thanks for helping me, I've only been seriously trying to learn for about 2 weeks now, but sometimes i just forget parts. I'm gonna take your suggestion and see if I can remove the destructor and write in a value-setting function within the constructor. thanks again. |
| Aaronugio is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 14
| it worked thanks again so much |
| Aaronugio is offline | |
| | #8 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > SAMS Teach yourself C++ in 1 hour a day Should be "SAMS Teach yourself to tell the difference between C++ and a hole in the ground in 1 hour a day"
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Tags |
| operator, overloading, problem, vc++ |
| Thread Tools | |
| Display Modes | |
|