Undefined reference error

Hi there! I'm having issues with a function I'm writing to implement the days of the week in a program. I'm trying to make a default class constructor and and a constructor with a parameter that takes the string input and assigns it to an array. Then the function takes the class day and prints it on screen. I'm getting an error though, whenever I run the code I get this error.

https://i.imgur.com/qMYKQIa.png

I'm trying to see why it's saying the reference is undefined, and if I'm doing the constructors wrong. Thanks for any help.

implementation file:

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

#include "dayType.h"

using namespace std;

// array for days of the week


// part b
void dayType::print() const
{
    int i = currentDay;
    cout << "The day is";
    cout << days[i] << endl;
}

// part d
string dayType::nextDay() const {
    return "Test";
}

// part e
string dayType::prevDay() const {
    return "Test";
}

// part f
void dayType::addDay(int nDays) { }

// part a
void dayType::setDay() {
    return;
}

// part c
int dayType::getDay() const { 
return currentDay;
}

dayType::dayType() {
    days[0] = "Sunday";
	days[1] = "Monday";
	days[2] = "Tuesday";
	days[3] = "Wednesday";
	days[4] = "Thursday";
	days[5] = "Friday";
	days[6] = "Saturday";
    currentDay = 0;
    
    return;
}

// part g
dayType::dayType(string d) {
    days[0] = "Sunday";
	days[1] = "Monday";
	days[2] = "Tuesday";
	days[3] = "Wednesday";
	days[4] = "Thursday";
	days[5] = "Friday";
	days[6] = "Saturday";
    currentDay = 0;
    
    if (d == "Sunday")
       currentDay = 0;
    else if (d == "Monday")
       currentDay = 1;
    else if (d == "Tuesday")
       currentDay = 2;
    else if (d == "Wednesday")
       currentDay = 3;
    else if (d == "Thursday")
       currentDay = 4;
    else if (d == "Friday")
       currentDay = 6;
    else if (d == "Saturday")
       currentDay = 7;
    else
    {
        cout << "Invalid day entered. Setting to Sunday.";
        currentDay = 0;
    }
    return;
}


Header file:

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
#ifndef H_dayType 
#define H_dayType
 
#include <string>


using namespace std;

class dayType
{
public:
    static string days[7];
    void print() const;
    string nextDay() const;
    string prevDay() const;
    void addDay(int nDays);
    void setDay();
    int getDay() const;
    dayType();
    dayType(string d);

private:
    int currentDay;
};

#endif 


Main program:

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
#include <iostream>
#include <string>
  
#include "dayType.h" 
 
using namespace std;

int main() 
{
    dayType TestDay;
    //dayType myDay("Monday");
    //dayType temp("Sunday");
    
    TestDay.print();
    //myDay.print();
    cout << endl;

    //cout << myDay.prevDay() << endl;

    //cout << myDay.nextDay() << endl;

    //temp.print();
    //cout << endl;

    //cout << temp.prevDay() << endl;

    //cout << temp.nextDay() << endl;

    return 0;
}

A static class variable has to be created somewhere. It doesn't get created when you create an instance of the class.

Put string dayType::days[7]; in the class implementation file, outside of any functions.
And don't forget that you can initialize that static variable when you initialize it outside the class.

Since it is static you really don't want every constructor to initialize that variable.

1
2
std::string dayType::days[]{"Sunday", "Monday", "Tuesday", "Wednesday",
                            "Thursday", "Friday", "Saturday"};
Topic archived. No new replies allowed.