"Identifier not found" - outside file / function problem


This program is supposed to have users input a day of the week and time and have it return what was inputted at the end. I've had problems thus far with calling my other file and it working. The error I'm getting with the current code is:

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>timeEntry.cpp
1>Generating Code...
1>Linking...
1>timeEntry.obj : error LNK2005: "int __cdecl fnTimeStart(void)" (?fnTimeStart@@YAHXZ) already defined in main.obj
1>timeEntry.obj : error LNK2005: "int iAMPM" (?iAMPM@@3HA) already defined in main.obj
1>timeEntry.obj : error LNK2005: "int * iTimes" (?iTimes@@3PAHA) already defined in main.obj
1>C:\Documents and Settings\-\My Documents\Visual Studio 2008\Projects\test\Debug\test.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Documents and Settings\-\My Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm"
1>test - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


main.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
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
#include <iostream>
#include <string>
#include "timeEntry.cpp"

using namespace std;

string doW(int iDayNum)
{
	switch(iDayNum)
	{
		case 1:
			return "Monday";
			break;
		case 2:
			return "Tuesday";
			break;
		case 3:
			return "Wednesday";
			break;
		case 4:
			return "Thursday";
			break;
		case 5:
			return "Friday";
			break;
		case 6:
			return "Saturday";
			break;
		case 7:
			return "Sunday";
			break;
		default:
			return "WRONG";
			break;
	}
}
int main ()
{
	// Defining variables
	int iNum; string sDOW;
	iNum = 0;

	//long int iTimeStart; long int iTimeFinish;

	// Algorithm to determine desired day
	cout << "Enter a number between 1 and 7:\n";
	cin >> iNum;
	sDOW = doW(iNum);
	while (sDOW == "WRONG")
	{
		cout << "You have selected an incorrect value\nEnter a number between 1 and 7:\n";
		cin >> iNum;
		sDOW = doW(iNum);
	}
	cout << "You have selected " << sDOW << "\n";

	fnTimeStart();
/*
	// Algorithm to determine starting time period
	cout << "Please select a starting time (Military clock):\n";
	cin >> iTimeStart;
	while (!(iTimeStart > 0 && iTimeStart <= 2400))
	{
		cout << "You have entered an invalid starting time\nPlease select a starting time (Military clock):\n";
		cin >> iTimeStart;
	}

	cout << "You have selected " << iTimeStart << "\n";

	// Algorithm to determine starting time period
	cout << "Please select an ending time (Military clock):\n";
	cin >> iTimeFinish;
	while (!(iTimeFinish > 0 && iTimeFinish <= 2400) || iTimeFinish <= iTimeStart)
	{
		cout << "You have entered an invalid ending time\nPlease select an ending time (Military clock):\n";
		cin >> iTimeFinish;
	}

	cout << "You have selected " << iTimeFinish << "\n";

	// All the information
	cout << "\n\n\nYou have entered the following information\nDesired day: " << sDOW << "\n";
	cout << "Starting time: " << iTimeStart << "\nEnding Time: " << iTimeFinish << "\n";
*/
	return 0;
}






timeEntry.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
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

// Variable initialization
int iAMPM = 0; 
int iTimes[] = {0, 0};

extern int fnTimeStart()
{

	 // Inputting variables
	cout << "Enter 1 for AM or 2 for PM: ";
	cin >> iAMPM;
	while(iAMPM != 1 || iAMPM != 2)
	{
		cout << "\nYou have entered an invalid number\nPlease enter 1 for AM or 2 for PM: ";
		cin >> iAMPM;
	}

	cout << "\nPlease enter an hour of the day (1 through 12): ";
	cin >> iTimes[0];
	while(!(iTimes[0] >= 1 || iTimes[1] <=12))
	{
		cout << "/nYou have entered an invalid hour\nPlease enter an hour of the day (1 through 12): ";
		cin >> iTimes[0];
	}

	cout << "\nYou have entered " << iTimes[0] << "\n";

	cout << "\nPlease enter the minutes in the hour (0 through 59): ";
	cin >> iTimes[1];
	while(!(iTimes[1] >= 0 || iTimes[1] <=59))
	{
		cout << "/nYou have entered an invalid number of minutes\nPlease enter the minutes in the hour (0 through 59): ";
		cin >> iTimes[1];
	}

	cout << "/nYou have entered " << iTimes[1] << "\n";

	return 0;
}




Well, first off, why are you declaring fnTimeStart() as returning an extern int? Or is this something I just have never seen before...? As for the other linker errors, are these the only files in your project?
I have fnTimeStart() as an extern int because it kept saying that it couldn't find the function (I can remove it if you'd like)

These are the only two files in the project, main.cpp and timeEntry.cpp
The problem is that your main.cpp is #including timeEntry.cpp and then you are also linking againt timeEntry.obj, so your executable ends up with two copies of everything in timeEntry.cpp.

Do not include .cpp files. Only include header files (.h).

Make a timeEntry.h which has in it "extern int fnTimeStart();" and #include the header file in main.cpp instead of timeEntry.cpp.
I just want to have an outside cpp file contain a function, do I really need to create a header file in order to call it ?
It is improper to #include cpp files directly. You could get away without a header file by adding a prototype for the function in main.cpp, but this is
anti-C++ and not recommended.

Bite the bullet and create the header file. It should take you all of 2 minutes unless you type really slow.
Topic archived. No new replies allowed.