Accessing arrays in a class

I'm new to classes and this is my first time using an array within my class. I'm trying to access my array with the print function but when I do I'm getting this error:
Undefined symbols for architecture x86_64:
"DayOfYear::months", referenced from:
DayOfYear::print() in main-24ee56.o
"DayOfYear::monthDays", referenced from:
DayOfYear::print() in main-24ee56.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


I assume I'm calling the array incorrectly?

Here's my class 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
  #ifndef DaysOfYearClass_H
#define DaysOfYearClass_H

class DayOfYear
{
    private:
        int day;
    public:

        static string months[12];

        static int monthDays[12];

        DayOfYear(int d)
        {
            day = d;
        };
        
        ~DayOfYear(){};

        void setDay(int d)
        {
            day = d;
        };

        void print()
        {  
           if ( day < 32)
           {
               cout << DayOfYear::months[0] << "    " << day << endl;
           }

           if (day > 31 && day < 60 )
           {
               cout << DayOfYear::months[1] << "    " << day - monthDays[0] << endl;
           }
        };
};
#endif 


Here's my main.cpp 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
#include <iostream>
#include <string>
using namespace std;
#include "DaysOfYearClass.h"

int main() // loop this program for multiple inputs
{
    // Create an instance of the DayOfYear class
    DayOfYear dayOfYearObj;

    int day; // To hold the day
    int monthDays[12] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; 
    string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
     

    // Display the purpose of the program.
    cout << "This program converts a number \n"
    << "into a string representing the \n"
    << "month and day.\n\n";

    // Get the day as input from the user.
    cout << "\nEnter a whole number between 1 and 365: ";
    cin >> day;

    // Set the day.
    dayOfYearObj.setDay(day);

    // Display the object.
    dayOfYearObj.print();

    return 0;
}
Last edited on
You're trying to create static class variables. Static class variables are shared by every instance of that class. In effect, no matter how many instances of the class you create, there will only be ONE instance of the static class variable, and they all use the same one.

This means that creating an instance of the class does NOT create the static class variable. Of course it doesn't; they're all meant to use the same one so they can't go around creating extra instances of the static class variable.

So YOU have to create the static class variable, once, yourself.

A convenient place to do this is in the class *.cpp file, but since your class doesn't have a *.cpp file, you can do it at the start of main.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;
#include "DaysOfYearClass.h"

// Create static class variables here
string DayOfYear::months[12];
int DayOfYear::monthDays[12];


int main() // loop this program for multiple inputs
{
  ...
 
Last edited on
Ok I see what you're saying but do I need to even make the arrays static? My instructions just want the arrays in the class.
You do not have to make the arrays static
Topic archived. No new replies allowed.