Header file Program Won't Compile

I have a program that I can't get to compile. I have problem with Employee.cpp but I can't figure out how to fix it.

Here are the requirements for the program.

Create a C++ class called Employee which will implement the Employee constructor. Remember that once the Name of the Employee is entered and the Address of the Employee is entered, once you add an SSN you are ready to create an employee.


Here what the Name Header file (Name.h) will have:


1- a default constructor

2- 3 private string instance variables for :First Name, Middle Name, and a last Name

3- getFirstLast function: it returns the first and middle name and last name in order

4- print function: it prints the first, middle, and last name.



Here what the Address Header File (Address.h) will have:


1- a default constructor

2- 4 private string instance variables for :Street, City, State, Zip

3- getCity: it returns the City

4- getState: It returns the State

5- getStreet: It returns the street

6- getZip: it returns the zip

7- print function: it prints the Street, City, State, and Zip.


Here what the Employee Header File (Employee.h) will have:



1- 3 private instance variables: One for Name (Use the Header Name from above), One for The Address

(Use the Address Header from above), a string variable for the SSN.

2- a default constructor which initializes the SSN to "999-99-9999", Name to "John H. Doe", Address to

"99999 Sunset Boulevard" , "Beverly Hills", "CA", "99999"

3- a constructor with 3 parameters

4- getName function: it returns the Name of the Employee

5- getAddress function : it returns the address of the Employee.

6- getSSN function: it returns the SSN as a string

7- print function: Prints the name, prints the Address, prints the SSN.


Implement the Employee class using the constructor as an external function:



1- Remember that the default constructor for Employee has the following initial values:

SSN to "999-99-9999", Name to "John H. Doe", Address to "99999 Sunset Boulevard" ,

Beverly Hills, CA, 99999

2- Remember that the default constructor for Address has the following initial values:

Address to "99999 Sunset Boulevard" , "Beverly Hills", "CA", "99999"

3- Remember that the default constructor for Name has the following initial values:

Name to "John H. Doe",

In the void main() function you would declare:

a Name n;

an Address A;

and an Employee e;

and print the values of each.

Remember to compile the Employee.cpp file and execute it to verify the output:

Name:

John H. Doe

Address:

9999 Sunset Boulevard" , Beverly Hills, CA, 99999 (Address)

Employee:

John H. Doe

99999 Sunset Boulevard" , Beverly Hills, CA, 99999

xxx-xx-xxxx


Here are the errors I am getting:

1>c:\users\eric\documents\visual studio 2010\projects\employee\employee\employee.cpp(19): error C2065: 'SSN' : undeclared identifier
1>c:\users\eric\documents\visual studio 2010\projects\employee\employee\employee.cpp(19): error C2146: syntax error : missing ';' before identifier 'ssn'
1>c:\users\eric\documents\visual studio 2010\projects\employee\employee\employee.cpp(19): error C2065: 'ssn' : undeclared identifier
1>c:\users\eric\documents\visual studio 2010\projects\employee\employee\employee.cpp(29): error C2065: 'ssn' : undeclared identifier
1>c:\users\eric\documents\visual studio 2010\projects\employee\employee\employee.cpp(30): error C2065: 'ssn' : undeclared identifier

Any help is appreciated.

Here is the code for my program.

Employee.cpp
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
  #include "StdAfx.h"
#include "Name.h"
#include "Address.h"
#include "Employee.h"


using namespace std;


//entry point of program
void main()
{
	//create Name, Address, and Employee object

	Name n;
	Address A;
	Employee e;
	SSN ssn;

	
	//print out defaults of an employee object

	e.prtEmployee(); 
	

	Name n1 ("George", "Victor", "Meghabghab");
	Address a1 ("3709 Hoch Court", "Murfreesboro", "TN", "37128");
	ssn = "987-65-4321";
	Employee e1 (ssn,n1, a1);

	e1.prtEmployee();

	cin.get();

}


Employee.h

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
#ifndef Employee_H 
#define Employee_H

#include <iostream>
#include <iomanip>
#include <string>
#include "Address.h"
#include "Name.h"

using namespace std;

class Employee
{

private:
	Address A;
	Name n;
	string SSN; // string variable for Social Security Number

public:
	Employee()
	{
		Name n("John", "H.", "Doe");
		Address A("99999 Sunset Boulevard", "Beverly Hills", "CA", "99999");
		SSN = "999-99-9999";
	}
	Employee(Name &n1, Address &a1, string ssn)
	{
		SSN = ssn;
		n = n1;
		A = a1;
	}
	~Employee(){;} // destructor
	Name getName()
	{
		return n;
	}
	Address getAddress()
	{
		return A;
	}
	string getSSN()
	{
		return SSN;
	}
	void prtEmployee()
	{
		n.printName();
		A.printAddress();
		cout << SSN << endl;
	}
};

#endif; 


Address.h
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
#ifndef Address_H 
#define Address_H

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class Address

{

private:
			string Street;
			string City;
			string State;
			string Zip ;
			
public:
	Address()
	{
		Street = "99999 Sunset Boulevard";
		City = "Beverly Hills";
		State = "CA";
		Zip = "99999";
	}

	Address(string St, string Ct, string st, string zip)
	{
		Street = St;
		City = Ct;
		State = st;
		Zip = zip;
	}

	~Address(){;}       //destructor
	string getCity(){return City;}
	string getState(){return State;}
	string getStreet(){return Street;}
	string getZip(){return Zip;}
	void printAddress()
	{ 
	cout << City << ' '<< State << ' ' << Street << ' ' << Zip << endl;
	}
};

#endif



Name.h
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
#ifndef Name_H 
#define Name_H


#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

class Name
{
private:
	string First;
	string Middle;
	string Last;
public:
	Name() // Default constructor
	{
		First = "John";
		Middle = "H.";
		Last = "Doe";
	}
	Name(string Fi, string Mi, string La)
	{
		First = Fi;
		Middle = Mi;
		Last = La;
	}
	~Name(){;}
	string getFirst() // returns First name
	{
		return First;
	}
	string getMiddle() // return Middle name
	{
		return Middle;
	}
	string getLast() // returns Last name
	{
		return Last;
	}
	void printName() // prints full name
	{
		cout << First << " " << Middle << " " << Last << endl;
	}


};


#endif 
You should not put a semicolon after preprocessor directives. #endif;

In main you are trying to declare a variable (ssn) of type SSN but the type SSN has not been declared anywhere.
Yeah I was suppose to declare it as a String, but it's been giving me issues. I think my problem is I'm not sure where/how to declare the SSN variable.
Currently, you have SSN declared as a private data member of Employee. That's fine.

What are you trying to do at at line 18 or Employee.cpp?
This is a school assignment. My instructor wants void main() used.. So I have SSN declared as a private data member. Why i am I getting an undeclared identifier error for SSN? Even if I change it to String SSN it gives me all those same errors. I have asked him form help he said change Employee e1(SSN, n1,a1). There was a semicolon I took out as someone else suggested. I did that that and I still get the same errors.
Last edited on
Why i am I getting an undeclared identifier error for SSN?

This has already been explained. You're trying to declare a variable of type "SSN", but SSN isn't a type. You haven't defined a type "SSN" anywhere in your code.

Even if I change it to String SSN it gives me all those same errors

Can you be more precise? What errors are you getting? You can't be getting the exact same errors, because, if I understand you correctly, you're no longer trying to use SSN as a type.

Remember that C++ is case-sensitive. "string" is not the same as "String".
In employee.h you used
1
2
#include "Address.h"
#include "Name.h" 

But then you reincluded them on employee.cpp. You should remove lines
1
2
#include "Address.h"
#include "Name.h" 

from employee.cpp
Last edited on
You could argue that, since Employee.cpp explicitly uses the Name and Address types, those headers should still be explicitly included. That way, in the (unlikely) event that the implementation of Employee changes to no longer use those types, and the headers are removed from Employee.h, then Employee.cpp will still compile.

OTOH, main only uses those types because they're part of the Employee interface, so it's vanishingly unlikely that, were this to happen, you'd still want to use them in main.

Bottom line: it doesn't matter. Leaving them in causes no harm, and taking them out causes no harm. No need to overthink it :)
Last edited on
I made a minor change I added #include<string>to Employee.cpp and I changed String to string in that file. However I am getting a new error (the only one now). It is "1>c:\users\eric\documents\visual studio 2010\projects\employee\employee\employee.cpp(31): error C2664: 'Employee::Employee(Name &,Address &,std::string)' : cannot convert parameter 1 from 'std::string' to 'Name &' ".
The Employee constructor expects a Name object but you try to pass a std::string object? If you don't want to change the Employee class you have to construct a Name object that you can pass to the Employee constructor.
Topic archived. No new replies allowed.