Strange Errors that Can not understand because of String Variable

Hello, everyone:

This is a modified version of my 3 files program, but seem it stil not working...
the erros messages are:
Error 1 error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * dayName" (?dayName@@3PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) already defined in main.obj C:\Users\...\implementation.obj

Error 2 error LNK1169: one or more multiply defined symbols found C:\Users\...\Debug\S2012hw2simple.exe 1

SourceFile-->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
#include <iostream>
#include <string>
#include "dayType.h"

using namespace std;

void printDay(int day);
void getDay (int day);
void printNextDay (int day);
void printPreviousDay (int day);




int main()
{
	//initialization
	dayType runday;
	int todayDay;
	char Go;
	while (true)
	{
		cout <<"Please input today's Day (0=Sunday, 1=Monday..etc):"<<endl;
		cin >>todayDay;

		cout << "inputed todayDay is :"<< todayDay<<endl;
		runday.printDay (todayDay);
		runday.printNextDay (todayDay);
		runday. printPreviousDay ( todayDay);
		cout<<"Enter a number then I will get you the exact day after the days you specified: ";
		cin >> todayDay;
		runday. forcastGetDay( todayDay);
		cout <<"Do You Want More Calculations? (y/n): "<<endl;
		cin >> Go;
		if (Go=='n')
		{
			system("PAUSE");
			return 0;
		}
	}
}



implementation.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
#include <iostream>
#include <string>
#include "dayType.h"

using namespace std;

string dayName [7] = {"Sunday","Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday"};

void dayType::printDay(int day)
{
	cout << "Today's Day is: "<<dayName[day]<<endl;	
	
}

void  dayType::printNextDay (int day)
{
	if (day =7)
		day =1;
	
	cout << "Tomorrow's Day is: "<<dayName[day]<<endl;	

}

void  dayType::printPreviousDay (int day)
{
	if (day =0)
	day =7;
	
	cout << "Yesterday was : "<<dayName[day]<<endl;	
	
}


void  dayType::forcastGetDay (int day)
{
	//forcase mechanism
	if (day <=7 && day >=1)
	{
		printDay (day);
	}
	else if (day >=8)
	{
		day = day%7;
		cout << "The day will be "<< dayName[day]<<" , is that right?"<<endl;

	}
	else
	{
		cout << "Invalid Input! Program Failed!";
	}

}


Header file->dayType.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class dayType
{
	public:
		//public functions...
		void printDay(int);
		void printNextDay (int);
		void printPreviousDay (int);
		void forcastGetDay (int);
		//string dayName[7];
		extern string dayName [7];
	private:
		//private functions hide from users direct access.
		int day;
	
};

string dayName is a global variable in implementation.cpp. It is not part of the class.

Take extern string dayName[7]; out of the class definition and make it global.

OR

get rid of the extern keyword and initialize dayName in dayType's constructor.
Hi, Stwbond:

I moved extern string dayName[7]; out of class and made it global in dayType.h.

But it does not work and triggered lots more errors.

So I tried to give constructor to header file:

is it write as inside public and
string dayName(int);

Is that correct?
Topic archived. No new replies allowed.