How to access a nested classes data members if it is a pointer in the outer class

Hello, everyone. I am very much still a noob programer and was seeking some help with how to access a nested classes data members if the outer class holds a pointer to the nested class.

I have two classes, Job and JobException. if Job receives input that is invalid for costOfJob it creates a JobException and throws it.

The first catch block in the Main function at the bottom of my code is where the problem is. I am not sure of the syntax to access the JobException.*Job.DataMembers or if it is possible.

also when I intialize JobEcxeption, which requires a Job pointer, I use JobException errorJob(this). Is this the correct way to send a pointer of the class you are in to another class?

I'm not opposed to a work around or redoing it if my code is inefficient. Pointers aren't my strong suit. Any and all feedback is appreciated. Thanks for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Purpose:   demonstrate basic knowledge of a try, throw, and catch using //Job and JobException class
// Author:    
// Date:       9/5/2014
// Filename:   Job.cpp
// Input:       job# and cost of job
// Output:     job#, cost of job and an error message should input be invalid

#include<iostream>
#include<string>
#include<exception>
using namespace std;
class JobException;
class Job;

class JobException : public runtime_error
{
public:
	JobException(Job*);
	Job* errorJob;
	const string ERROR_MSG = " value is out of range.";
};
JobException::JobException(Job* e) : runtime_error(ERROR_MSG)
{
	errorJob = e;
}


class Job
{

	friend istream& operator>>(istream&, Job&);
	friend ostream& operator<<(ostream&, const Job&);
public:
	void setJobId();
	void setJobCost();
	void setId(int);
	void setCost(double);
	int getId();
	double getCost();

private:
	int idNum;
	double costOfJob;

};
void Job::setJobId()
{
	cout << endl; // to clear the buffer
	cout << "Enter Job number: ";
	cin >> idNum;
}
void Job::setJobCost()
{
	cout << "Enter the cost of the job: ";
	cin >> costOfJob;
	if (costOfJob < 250)
	{

		JobException errorJob(this);
		throw errorJob;
	}
}
void Job::setCost(double c)
{
	costOfJob = c;
}
void Job::setId(int j)
{
	idNum = j;
}
int Job::getId()
{
	return idNum;
}
double Job::getCost()
{
	return costOfJob;
}

istream& operator>>(istream& in, Job& aJob)
{
	aJob.setJobId();
	aJob.setJobCost();
	cout << endl << " Thank you!" << endl;
	return in;
}
ostream& operator<<(ostream& out, const Job& aJob)
{
	out << "Job id: " << aJob.idNum << endl << "Job cost: $" << aJob.costOfJob << endl;
	return out;
}



int main()
{

	Job aJob;

	try
	{
		cin >> aJob;
	}

	catch (JobException e)
	{

		do
		{
			cout << endl << e.errorJob.getCost() << e.ERROR_MSG << endl;
			cout << "Please enter a cost above $250." << endl;
			e.errorJob.setJobCost();


		} while (e.errorJob.getCost < 250);

	}
	catch (...)
	{
		cout << "Error.";
	}

	cout << aJob;

	system("Pause");
	return 0;
}
e.errorJob->setJobCost();
Last edited on
wow, thanks! It was so simple. You saved me a lot of headache trying to figure this one out.
Topic archived. No new replies allowed.