Can somebody help me figure out what I did wrong with my default constructor declaration
I'm getting an error message no appropriate constructor available
Thanks

TimeOff.h
class TimeOff
{
private:
	string name;
	int id;
	NumDays maxSickDays;
	NumDays sickTaken;
	NumDays maxVacation;
	NumDays vacTaken;
	NumDays maxUnpaid;
	NumDays unpaidTaken;
public:
        //default constructor
	TimeOff (string=" ", int=0, double=0, double=0, double=0, double=0,
		double=0, double=0);
	void setMaxSickDays(double);
        void setSickTaken(double);
	void setMaxVacation(double);
	void setVacTaken(double);
	void setMaxUnpaid(double);
	void setUnpaidTaken(double);
	double getMaxSickDays() const
	{return maxSickDays.getDays();}
	double getSickTaken() const
	{return sickTaken.getDays();}
	double getMaxVacation() const
	{return maxVacation.getDays();}
	double getVacTaken() const
	{return vacTaken.getDays();}
	double getMaxUnpaid() const
	{return maxUnpaid.getDays();}
	double getUnpaidTaken() const
	{return unpaidTaken.getDays();}
};

TimeOff.cpp

TimeOff::TimeOff(string n, int empid, double maxSick, double sTaken, double maxVac,
				 double vTaken, double maxUnpd, double uTaken)
{
	//check if string is empty
	if(!n.empty())
	//assign name
		name=n;
	//assign employee id
	empid=id;
	//initialize max sick days to default
	maxSickDays.setHours(maxSick);
        

}

Dani AI

Generated

A few focused points that address the usual causes of "no appropriate constructor available" and the specific issues visible in the posts by and .

First, the most common causes are (a) a declaration/definition mismatch (types, const/ref qualifiers, or default-argument placement), or (b) a member field that the compiler cannot construct because it has no matching constructor (this is often the real culprit when a class contains other class-type members such as NumDays). The compiler error text will normally name the missing constructor (for example, it will mention NumDays::NumDays() or NumDays::NumDays(double)), so read that line first to see which type needs attention.

A few concrete, non-redundant checks and fixes:

  • Do not repeat default arguments in the .cpp definition; put them in the header (declaration) only. Declarations and out-of-line definitions must match in parameter types.
  • The compiler generates an implicit default constructor only when no constructors are declared at all. Declaring any constructor (including one with all default parameter values) suppresses the implicit default ctor.
  • Initialize any member objects that do not have a no-argument constructor in the constructor initializer list, or add a suitable constructor to that member type. Members that are const or references must also be initialized in the initializer list.
  • Fix the obvious assignment bug in the implementation: the employee id should be assigned to the member, not the other way around:
id = empid;

Finally, do a clean rebuild after changes to avoid stale object files. Addressing the assignment bug above and ensuring every NumDays member is either default-constructible or explicitly initialized will very likely resolve the reported error.

Your .h file and .cpp file must match each other. You're implementing the constructor just about fine (in .cpp file) but are missing the declaration for that constructor.

Declare these ( in .h file)

TimeOff(); // this is the default constructor
TimeOff(string n, int empid, double maxSick, double sTaken, double maxVac,
				 double vTaken, double maxUnpd, double uTaken); // this is the constructor which takes parameters

And implement them like this ( using the constructor list )

TimeOff::TimeOff() : 
 name(""), id(0), maxSickdays(0), sickTaken(0), maxVacation(0), vacTaken(0), maxUnpaid(0), unpaidTaken(0) 
{}
// this is the implementation for the default constructor

TimeOff::TimeOff(string n, int empid, double maxSick, double sTaken, double maxVac,
				 double vTaken, double maxUnpd, double uTaken) :
name(n) , id(empid), maxSickdays(maxSick), sickTaken(sTaken), maxVacation(maxVac), vacTaken(vTaken), maxUnpaid(maxUnpd), unpaidTaken(uTaken)
{}
// this is the implementation for the constructor that takes parameters

Btw, if there is no default constructor available ( constructor with no parameters ), a compiler will generate one automatically.

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.