Header Files and Using Methods Located in Other .ccp Files

I am getting an undeclared identifier error on lines 8, 15, and 16 of on Will_lib.cpp

it shows up here as line 50, 57, and 58

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
 // overtime.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include "Will_lib.h"
#include <iomanip>
#include "Will_lib.cpp"
using namespace std;

double OvertimePay(double, double);

int main()
{
	cout << fixed << setprecision(2);//displays 2 decimal places
	double HOURS{ 0.0 };// stores hours
	double pRate{ 0.0 };// stores pay rate
	double GROSSPAY{ 0.0 };// stores Gross pay
	std::cout << "To calculate Gross Pay first enter hours worked.\n:"; //displays instructions

	HOURS = getHoursWorked();//stores hours worked when user enters
	
	pRate = getPayRate();// stores pay rate when user enters
	if (HOURS > 40)
	{
		GROSSPAY = OvertimePay(HOURS, pRate);
	}
	else
	{
		GROSSPAY = calcGross(HOURS, pRate);// calculates and stores gross pay
	}
	cout << "\n\n Your Gross Pay is " << GROSSPAY << "$. \n\n";//displays gross pay

}
double OvertimePay(double HOURS, double pRate)
{
	double GROSSPAY{ 0 };//stores gross pay
	GROSSPAY = (HOURS-(HOURS-40)) * pRate;//calculates gross pay
	GROSSPAY += (HOURS - 40)*(pRate*1.5);
	return GROSSPAY;//returns gross pay
}

//Will_lib.cpp

#include "pch.h"

double getHoursWorked()//function for entering and returning hours worked
{
	double HOURS1{ 0.0 };//stores hours
	cin >> HOURS1;//takes hours
	return HOURS1;//returns hours
}

double getPayRate()//function for entering and returning pay rate
{
	double rate{ 0 };//stores pay rate
	cout << "\n Next enter the Pay Rate.\n: ";// gives user instructions
	cin >> rate;//takes user enterd pay rate
	return rate;//returns pay rate
}

double calcGross(double HOURS, double pRate)//function for calculating and returning Gross pay
{

	double GROSSPAY{ 0 };//stores gross pay
	GROSSPAY = HOURS * pRate;//calculates gross pay
	return GROSSPAY;//returns gross pay
}

//Will_lib.h

double getHoursWorked();//prototype for function
double getPayRate();//prototype for function
double calcGross(double, double);//prototype for function 
Last edited on
At the top of Will_lib.cpp, add:
1
2
#include "Will_lib.h"
#include <iostream> 
Assuming it's the symbols cin and cout that are causing the errors, you need to do 2 things:

1) In Will_lib.cpp, include <iostream>, which contains the definitions of those symbols
2) Qualify them with the namespace, i.e. use std::cin and std::cout (just as you've done at line 19).
Last edited on
> I am getting an undeclared identifier error
¿which identifier? (¿why do you withhold information?)

> it shows up here as line 50, 57, and 58
1
2
3
cin >> HOURS1; //50
cout << "\n Next enter the Pay Rate.\n: ";// 57
cin >> rate;//takes user enterd pay rate //58 
don't see any problem there and can compile fine.
edit: well, actually there is a problem.
your source files should be self-contained, Will_lib.cpp needs to include iostream because it uses cin and cout


> #include "Will_lib.cpp"
don't
http://www.cplusplus.com/forum/general/140198/
http://www.cplusplus.com/forum/general/113904/
Last edited on
Topic archived. No new replies allowed.