Donald Neal
Donald Neal

Reputation: 13

How to resolve a lot of errors in my C++ header file?

The goal of this program is to take an integer from 1 - 365 and convert it into a month and day. Ex. 2 = January 2nd, 365 = December 31st. I thought I got everything typed out correctly, I was on the right track but now I have like 16 errors coming from lines 12-14 in my header file, and 1 error coming from my class file.

I'm not sure if it's even truly the header or if I messed up somewhere else.

HEADER FILE

#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H

class DayOfYear
{
private:
    int day;

public:
    DayOfYear();
    static int daysAtEndOfMonth[];
    static string monthName[];
    void print();
    void setDay(int day) { this->day = day; };

};

#endif

CLASS

#include "DayOfYear.h"
#include <iostream>
#include <string>

using namespace std;

DayOfYear::DayOfYear() {

}

int DayOfYear::daysAtEndOfMonth[] = {
31, 59, 90,
120, 151, 181,
212, 243, 273,
304, 334, 365
};

string DayOfYear::monthName[] = { "January", "February",
"March", "April",
"May", "June"
"July", "August",
"September", "October",
"November", "December"
};

void DayOfYear::print() {
    int month = 0;

    while (daysAtEndOfMonth[month] < day)
        month = (month + 1) % 12;


    if (month == 0) {
        cout << "\nJanuary" << day << endl << endl;
    }

    else
    {
        cout << endl << monthName[month] << " " << day - daysAtEndOfMonth[month - 1]
            << "\n\n";
    };
};

MAIN

#include <iostream>
#include <string>
#include "DayOfYear.h"

using namespace std; 

int main() {
    int day; 
    DayOfYear DayOfYearObj;

    cout << "This program will convert an integer between 1 and 365 to a Month/Day format"
        << endl << endl;
    cout << "Please enter an integer from 1 to 365:";
    cin >> day;

    //set day
    DayOfYearObj.setDay(day);

    //display
    DayOfYearObj.print();

    return 0;

}

Upvotes: 0

Views: 565

Answers (1)

Your header file needs to #include <string>, and you must refer to std::string in the header file.

(Using using namespace std; is bad enough in a C++ file, it is much, much worse in a header file - it brings the whole of the std namespace into the global namespace in every file that includes your header. Just say no!)

Other comments:

  • daysAtEndOfMonth and monthName should be const.
  • Next time, include the error message (this error was instantly obvious, the next one won't be).
  • Next time, put a comment marking which is line with the error - I'm not going to take the time to count down to line 45.

Upvotes: 2

Related Questions