C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-20-2009, 10:00 AM   #1
Registered User
 
Join Date: Jul 2009
Posts: 14
Question new to C++ and I don't understand what it means...

Working in VC++ and here's my problem, was reading through my "SAMS Teach yourself C++ in 1 hour a day" and got to chapter 13 which is supposed to teach operator overloading, but when I modified the code from the book, as such:

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;
}
it gives me the errors:

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   Reply With Quote
Old 07-20-2009, 10:03 AM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
You know what () means -- it means function call.

This:
Code:
number newnumb;
is a declaration of a number object. This:
Code:
number newnumb();
is a declaration of a function that returns a number object.
tabstop is offline   Reply With Quote
Old 07-20-2009, 10:10 AM   #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   Reply With Quote
Old 07-20-2009, 10:12 AM   #4
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
You have declared a destructor, but there is no implementation.
Code:
~number();
Where is the code for ~number()?

EDIT: Also, the code for your constructor is missing too.
bithub is offline   Reply With Quote
Old 07-20-2009, 10:15 AM   #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:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 07-20-2009, 10:20 AM   #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   Reply With Quote
Old 07-20-2009, 10:23 AM   #7
Registered User
 
Join Date: Jul 2009
Posts: 14
it worked thanks again so much
Aaronugio is offline   Reply With Quote
Old 07-20-2009, 10:24 AM   #8
and the hat of vanishing
 
Salem's Avatar
 
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   Reply With Quote
Reply

Tags
operator, overloading, problem, vc++

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 04:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22