Hi everyone!

When i compile my small piece of code it gives me the error C2512 : No appropriate default constructor available

this is my definition of class person:

// Person.h
// A class defining a person
#pragma once
#include <iostream>
#include <string>
#include <functional>
using std::cout;
using std::endl;
using std::string;

class Person
{
public:
	Person(string first, string second);
	// Less-than operator
	bool operator<(const Person& p)const ;

	// Get the name 
	string getName()const;
private:
	string firstname;
	string secondname;
};

Here is my implementation :

#include "Person.h"



Person::Person(string first = "", string second = "")
{
	firstname = first;
	secondname = second;
}


string Person::getName()const
{
	return firstname + " " + secondname;
}


bool Person::operator<(const Person& p)const
{
	if(secondname < p.secondname ||
		((secondname == p.secondname) && (firstname < p.firstname)))
		return true;

	return false;
}

When i combine the definition and implementation into a single file Person.h the error disappears. Im confused. Would some one please help?

What happens when you explicitly declare a destructor?

Person::Person(string first = "", string second = "")

Out of curiosity, what are you tying to accomplish with your parameters?

What happens when you explicitly declare a destructor?

Nothing changes. Still get the error C2512 : 'Person::Person' : No appropriate default constructor available

Person::Person(string first = "", string second = "")

Out of curiosity, what are you tying to accomplish with your parameters?

Yup, at the moment it does nothing. But i've planned to give meaningful default values later. But i dont think the error has to do anything with the empty string parameters. isnt it?

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

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

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.

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

Yup, at the moment it does nothing. But i've planned to give meaningful default values later. But i dont think the error has to do anything with the empty string parameters. isnt it?

No, I was just curious why you would take in parameters if you're going to redefine them anyways.

That's worked!:) Thanks for mike_2000_17 and mzimmers.

Glad to help. A good rule of thumb is to always supply your own:

- default constructor
- copy constructor
- destructor

For every class you define.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.