Class/string error

I am stuck on what to do. The compiler is giving me this error. Any help is appreciated. Thank you.

main.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * DayOfYear::months" (?months@DayOfYear@@2PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
: fatal error LNK1120: 1 unresolved externals


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
#include <iostream>
#include <string>
using namespace std;
class DayOfYear
{
public:
	void print(); // prints the day in the monday format ie day 2 would be january 2
	int day;
	int endMonth[13];
	static string months[12];
	void setMonthName();
	void setEndOfMonth();
	int getDay(int) {return day;};
};
int main()
{
	
	int d;
	DayOfYear dayOfYear;
	cout << "Enter a day within the year" << endl;
	cin >> d;
	
	while (d < 1 || d >365)
	{
		cout << "Invalid input. Please Enter a number within 1-365" << endl;
		cin >> d;
	}
	dayOfYear.getDay(d);
	dayOfYear.print();
	return 0;
}

void DayOfYear :: setEndOfMonth()
{
	endMonth[0] = 0;
	endMonth[1] = 31;
	endMonth[2] = 59;
	endMonth[3] = 90;
	endMonth[4] = 120;
	endMonth[5] = 151;
	endMonth[6] = 181;
	endMonth[7] = 212;
	endMonth[8] = 243;
	endMonth[9] = 273;
	endMonth[10] = 304;
	endMonth[11] = 334;
	endMonth[12] = 365;
}
void DayOfYear::setMonthName()
{
	months[0] = "January";
	months[1] = "February";
	months[2] = "March";
	months[3] = "April";
	months[4] = "May";
	months[5] = "June";
	months[6] = "July";
	months[7] = "August";
	months[8] = "September";
	months[9] = "October";
	months[10] = "November";
	months[11] = "December";
}

void DayOfYear:: print()
{
	int i =0;
	int remainder = 0;
	while( endMonth[i]<day)
		i++;
	remainder = endMonth[i] - day;
	cout << months[i-1] << " " << remainder << endl;
}
static class members has to be declared in the class body (like you have done with months) and defined outside the class body (similar to how you have defined the member functions).
 
string DayOfYear::months[12];
Thank you. I got it to run but now i am running into another problem. The console crashes after outputting remainder. What i want to do with my program is if a user inputs 32. It will output february 1. Here is my code.
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
#include <iostream>
#include <string>
using namespace std;

class DayOfYear
{
public:
	void print(); // prints the day in the monday format ie day 2 would be january 2
	DayOfYear(){day=0;}
	int day;
	int endMonth[13];
	static string months[12];
	void setMonthName();
	void setEndOfMonth();
	void getDay(int);
	//void retrn() {cout << "the day is " << day <<
};
void DayOfYear:: getDay(int d)
{
	day = d;
}
string DayOfYear::months[12];
int main() 
{
	
	int d = 0;
	DayOfYear dayOfYear;
	
	cout << "Enter a day within the year" << endl;
	cin >> d;
	
	while (d < 1 || d >365)
	{
		cout << "Invalid input. Please Enter a number within 1-365" << endl;
		cin >> d;
	}
	dayOfYear.getDay(d);
	dayOfYear.print();
	return 0;
}

void DayOfYear :: setEndOfMonth()
{
	endMonth[0] = 0;
	endMonth[1] = 31;
	endMonth[2] = 59;
	endMonth[3] = 90;
	endMonth[4] = 120;
	endMonth[5] = 151;
	endMonth[6] = 181;
	endMonth[7] = 212;
	endMonth[8] = 243;
	endMonth[9] = 273;
	endMonth[10] = 304;
	endMonth[11] = 334;
	endMonth[12] = 365;
}
void DayOfYear::setMonthName()
{
	months[0] = "January";
	months[1] = "February";
	months[2] = "March";
	months[3] = "April";
	months[4] = "May";
	months[5] = "June";
	months[6] = "July";
	months[7] = "August";
	months[8] = "September";
	months[9] = "October";
	months[10] = "November";
	months[11] = "December";
}

void DayOfYear:: print()
{
	int i =0;
	int remainder = 0;
	while( endMonth[i] < day)
		i++;
	cout << "Value of I " << i << endl;
	cout <<  "value of day " << day << endl;
	remainder =   day - endMonth[i];
	cout << "Remainder " << remainder << endl;
	cout << months[i-1] << " " << remainder << endl;
}
Last edited on
For start, you cannot initialize a static member inside its class. You must do it outside. See below.


1
2
3
4
5
6
7
8
class example
{
    static std::string months[12];
};

std::string example::months[12] = {"January","February","March","April",
                                   "May","June","July", "August", "September",
                                   "October","November", "December"};




Anyway, Your code could have been done like this.



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
#include <iostream>
#include <string>

int main()
{
    const short endmonth[13] = {1,31,59,90,120,151,181,212,243,273,304,334,365};
    const std::string months[12] = {"January","February","March","April",
                              "May","June","July", "August", "September",
                              "October","November", "December"};
    short d = 0;

    std::cout << "Enter a day within the year" << std::endl;
    std::cin >> d;

    while (d < 1 || d > 365)
    {
        std::cout << "Invalid input. Please Enter a number within 1-365" << std::endl;
        std::cin >> d;
    }
    for(int i = 0; i < 12; ++i)
    {
        if(d >= endmonth[i] && d <= endmonth[i + 1])
        {
            std::cout << "Month is:" << months[i] << std::endl;
            break;
        }
    }
    return 0;
}
Last edited on
The console crashes after outputting remainder.


Look at line 84. What happens if i is 0?
Thank you for both your input. I combined what you guys said about my loop and my string. I fixed it and it worked. Thanks again.
Topic archived. No new replies allowed.