Hello everyone.

I need help using an instance of a class in another. Below are two header files. NumDays.h and TimeOff.h

I need to have instances of the NumDays class in the TimeOff class as members. When i try to create the instance called maxSickDays, i do not get access to the NumDays member functions. What is wrong with my code? Thanks in advance.

NumDays.h

#ifndef NUMDAYS
#define NUMDAYS
#include <iostream>

class NumDays
{

private:
	int hours;
	float days;
	float calcDays();

public:
	NumDays()
	{ hours = 0; days = 0.0; }

	void setHours(int hrs)
	{ hours = hrs; }
	int getHours()
	{ return hours; }
	float getDays();
	
	NumDays operator + ( const NumDays& );
	NumDays operator - ( const NumDays& );
	NumDays operator --();
	NumDays operator ++();
	NumDays operator ++ ( int );
	NumDays operator -- ( int );

};

#endif

and now TimeOff.H

#ifndef TIMEOFF
#define TIMEOFF
#include <iostream>
#include "NumDays.h"
#include <string>
using namespace std;

class TimeOff
{
private:
	string name;
	int idNum;
	NumDays maxSickDays;
		
public:
	TimeOff();
	

	void setName ( string n )
	{ name = n; }
	void setIdNum ( int id )
	{ idNum = id; }

	string getName()
	{ return name; }
	int getIdNum()
	{ return idNum; }
	
};

#endif

Thank you!

Can I see the part where you try to access the NumDays()?

Oops. I took all the calls to that when it would not work. Below is TimeOff.h where i am trying to use the maxSickDays instance and then its setHours member function. Thank you.

#ifndef TIMEOFF
#define TIMEOFF
#include <iostream>
#include "NumDays.h"
#include <string>
using namespace std;

class TimeOff
{
private:
	string name;
	int idNum;
	NumDays maxSickDays;
		
public:
	TimeOff();
	

	void setName ( string n )
	{ name = n; }
	void setIdNum ( int id )
	{ idNum = id; }

	void setMaxSickDays( int h )
	{ maxSickDays.setHours(h); }    /// trying to access this instance of NumDays


	string getName()
	{ return name; }
	int getIdNum()
	{ return idNum; }

	
	
};

#endif

I figured out my problem. When i defined the mutator and accessor functions in the cpp file instead of the header i was able to access the nested class' member variables and functions. Below are my revised header and cpp files.

TimeOff.h

#ifndef TIMEOFF
#define TIMEOFF
#include <iostream>
#include "NumDays.h"
#include <string>
using namespace std;

class TimeOff
{
private:
	string name;
	int idNum;
	NumDays maxSickDays;
	
		
public:
	TimeOff();
	

	void setName ( string n )
	{ name = n; }
	void setIdNum ( int id )
	{ idNum = id; }
	void setMaxSickDays( int );



	float getMaxSickDays();
	string getName()
	{ return name; }
	int getIdNum()
	{ return idNum; }


	
};

#endif

now TimeOff.cpp

#include "TimeOff.h"
#include "NumDays.h"
#include <iostream>
#include <string>
using namespace std;

TimeOff::TimeOff()
{
	name = "";
	idNum = 0;
	maxSickDays.setHours(0);
//	sickTaken.setHours(0);
//	maxVacation.setHours(0);
//	vacTaken.setHours(0);
//	maxUnpaid.setHours(0);
//	unpaidTaken.setHours(0);
}
void TimeOff::setMaxSickDays ( int hrs )
{
	maxSickDays.setHours(hrs);
	
}
float TimeOff::getMaxSickDays()
{
	return maxSickDays.getDays();
}
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.