Green coder here working on first C++ assignment and I'm seeking help on where I've gone wrong. Googled this error/situation and haven't found something close enough here to find the solution.
The assignment is to create a date class and then test it with a provided .cpp file. I wrote the whole thing out and compiled and got a depressing number of errors, so commented out much of it to debug the core first and I'm down to errors essentially saying the date.cpp can't find the variables defined in date.h. Googling seems to indicate that it's an include error but I have date.h included.
At one point, I had date.h defining month, day, year before capitalizing them, and the IDE (VS 2019 Community) suggested including (may have this misspelled) so I capitalized the variable names.
Thanks in advance for the help. I'm assuming (hoping?) I've got a conceptual issue here that's easy to tidy up.
// Program Name: date.h
#include <string>
#ifndef DATE_H
#define DATE_H
#endif
// Start of class
class Date {
public:
explicit Date(unsigned int = 1, unsigned int = 1, unsigned int = 2000); // default ctor setting to 1/1/2000 per specs
std::string tointString() const; // date string in month/day/year format
std::string toFullString() const; // date string in date fullMonth year format
~Date(); // Gotta have the dtor
// get functions
unsigned int getMonth();
unsigned int getDay();
unsigned int getYear();
std::string getMonthName() const;
private: // Core date data Book seems to want to use unsigned. Does this force non-negagive #s?
unsigned int Month; // month of year 1-12
unsigned int Day; // day of month 1-31 (leap year adjustments not required in this assignment)
unsigned int Year; // Will be using 1900 as the minimum year.
unsigned int checkDate(int, int); // Function to validate the days in a month
}; // end of Date
Here's date.cpp which is where the errors occur. The errors are in the three 'get' functions at the end. date.cpp(47,34): error C2065: 'Month': undeclared identifier
//
// Program Name; date.cpp
// Assignment: Date object assignment
//
#include "date.h" // class include file
#include <iostream> // standard output
#include <sstream> // formatted output
#include <string> // work with strings
#include <array> // needed for various arrays in get functions
using namespace std; // save all the scope fun
unsigned int const monthsInYear{ 12 }; // var just to make code more readable.
// Parameter based constructor w/ value checks
//
Date::Date(unsigned int mon, unsigned int dy, unsigned int yr)
// initialization list and start of value checking
: Month{ mon }, Day{ checkDate(dy , mon ) }, Year{ yr } {
if (mon < 1 || mon > monthsInYear) {
Month = 1; // if month out of range, set to 1 per specs
} // end of month value check if
if (yr < 1900 || yr > 2100) { // arbitrary upper limit
Year = 1900;
} // end of year value check if
} // end initialization list
int checkDate(unsigned int dayToCheck , unsigned int mon ) {
static array <unsigned int, monthsInYear> daysInMonth{ 31,28,31,30,31,30,31,31,30,31,30,31 }; // Array showing how many days in each month
unsigned int dayToReturn{ 1 }; // Value to return. Per specs, if dayToCheck is out of range, we will return 1.
if (dayToCheck >= 1 && dayToCheck <= daysInMonth[mon - 1]) { // needs to be month -1 re: index starts at 0
dayToReturn = dayToCheck; // dayToCheck inside the proper range, so set dateToReturn to that value.
} // end if block for day in month range check.
return dayToReturn; // returning the value.
} // end of dateCheck
// Functions for retrieving data
//
unsigned int getMonth() { return Month; }
unsigned int getDay() { return Day; }
unsigned int getYear() { return Year; }