following class ADate is declared enclosed by namespace A
.h file
#ifndef ADATE_H
#define ADATE_H
namespace A{
class ADate
{
public:
static unsigned int daysInMonth[];
private:
int day;
int month;
int year;
public:
ADate(const unsigned int day, const unsigned int month, const unsigned int year);
};
bool isValidDate(const unsigned int day, const unsigned int month, const unsigned int year);
}
#endif // ADATE_H
.cpp file:
#include "adate.h"
using namespace A;
unsigned int ADate::daysInMonth[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
ADate::ADate(const unsigned int day, const unsigned int month, const unsigned int year) :
day{day},
month{month},
year{year}
{
if(!isValidDate(day,month,year)){
throw string{"invalid Date"};
}
}
bool isValidDate(const unsigned int day, const unsigned int month, const unsigned int year)
{
if(month < 1 || month > 12){
return false;
}
if(day < 1 || day > ADate::daysInMonth[month-1]){
return false;
}
if(year < 1979 || year > 2038){
return false;
}
return true;
}
One should think that the code above can be compiled successfully. However, this is not that way, cause an undefined reference to `A::isValidDate(unsigned int, unsigned int, unsigned int)' occurs.
I don't understand why I have to use the namespace-specifier as prefix for the global function 'isValidDate'.
Can you explain why. Thank you