Thread: new to C++ and I don't understand what it means...

  1. #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...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #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.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    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).

  6. #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.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    14
    it worked thanks again so much

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Tags for this Thread