What happens when you explicitly declare a destructor?
mzimmers
Junior Poster in Training
81 posts since Oct 2011
Reputation Points: 10
Solved Threads: 8
Oh...I don't know how I missed this last night. A default constructor is one that contains no parameters, or has defaults provided for all parameters *in the definition.* Move the default assignments from your .cpp file to your .h file. I bet that helps.
Sorry...I guess I was pretty tired last night...I read it as "destructor" instead of "constructor."
mzimmers
Junior Poster in Training
81 posts since Oct 2011
Reputation Points: 10
Solved Threads: 8
Whenever you provide default parameters for a function (whether a constructor or otherwise) they should appear in the declaration of the function, not in its definition. In other words, your constructor declaration (in header file) should be:
public:
Person(string first = "", string second = "");
And your definition should start with:
Person::Person(string first, string second)
{
This should work. The error is explained by the fact that your class doesn't have a declared default constructor (a constructor that takes no parameter, or that has a default value for all parameters).
mike_2000_17
Posting Virtuoso
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
Aw, crud...I got definition and declaration backwards again, didn't I?
Sorry about that...but yeah, your defaults are in the wrong place.
I'm a little surprised that your compiler didn't furnish you with a default constructor; I thought that was part of the language.
mzimmers
Junior Poster in Training
81 posts since Oct 2011
Reputation Points: 10
Solved Threads: 8
>>I'm a little surprised that your compiler didn't furnish you with a default constructor; I thought that was part of the language.
The compiler will only furnish a default constructor if there are no user-defined constructors. As soon as a class has at least one constructor defined by the user, the compiler will not generate a default constructor.
mike_2000_17
Posting Virtuoso
2,136 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
Glad to help. A good rule of thumb is to always supply your own:
- default constructor
- copy constructor
- destructor
For every class you define.
mzimmers
Junior Poster in Training
81 posts since Oct 2011
Reputation Points: 10
Solved Threads: 8