Jump to content
  • Advertisement
Sign in to follow this  
marcmassacre

Did the constructor run yet???

This topic is 4521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 //class definition
class account
{	
	friend Date;
        private:
	int month;
	
	public:
	int print_dob();
	account(); 
		
};	//end class 

account::account()
{
	month=6;
	Date date1(6, month, 1972);

}

void account::print_dob()
{
date1.print();
}
The following code won't work because date1 is an undeclared identifier to member function print_dob(). Is there anyway to have print_dob() only run if if the constructor has been called first? You know what I mean?

Share this post


Link to post
Share on other sites
Advertisement
Do you mean for 'date1' to be local to the constructor? Also, why do the two print_dob()'s have different return types?

Share this post


Link to post
Share on other sites
well, if you wanted it to run right after the constructor has been called, you could call the method at the last line, after all of the member variables have been initialized:

account::account()
{
month=6;
Date date1(6, month, 1972);
print_dob();
}


Is this what you had in mind?

Share this post


Link to post
Share on other sites
I think I see what you are trying to say. Try adding date1 as a private member variable and then instantiate date1 within your constructor.

Share this post


Link to post
Share on other sites
There's no date1 object inside your class. You need to have one there in order for it to be accessed by the whole class, not just that function.

Share this post


Link to post
Share on other sites
I have to have the date1 instance in the constructor...So is there anyway to tell the compiler that print_dob can't be called unless the constructor has already been called??? thanks for youre help!!! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by marcmassacre
I have to have the date1 instance in the constructor...
Why? (Homework?)

Share this post


Link to post
Share on other sites

//class definition
class account
{
friend Date;
Date* date1;
private:
int month;

public:
int print_dob();
account();

}; //end class

account::account()
{
month=6;
Date temp = new Date(6, month, 1972);
date1 = &temp;
}

void account::print_dob()
{
date1->print();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!