I am trying to understand header(.h) file and source file(.cpp) in c++. I wrote a very simple example to understand it. My operation system is Mac OS. I run "g++ main.cpp" in terminal. But it occur compile error as below:
Undefined symbols for architecture x86_64:
"Date::Date(int, int, int)", referenced from:
_main in main-0f869d.o
ld: symbol(s) not found for architecture x86_64
My code is as below. Please help, thanks
The main.cpp file:
#include <iostream>
#include "Date.h"
using namespace std;
int main()
{
Date d(2017, 08, 20);
cout<<"getYear="<<d.getYear()<<endl;
return 1;
}
The Date.h file:
#ifndef DATE_H
#define DATE_H
class Date
{
private:
int m_year;
int m_month;
int m_day;
public:
Date(int year, int month, int day);
void SetDate(int year, int month, int day);
int getYear() { return m_year; }
int getMonth() { return m_month; }
int getDay() { return m_day; }
};
#endif
The Date.cpp file:
#include "Date.h"
// Date constructor
Date::Date(int year, int month, int day)
{
SetDate(year, month, day);
}
// Date member function
void Date::SetDate(int year, int month, int day)
{
m_month = month;
m_day = day;
m_year = year;
}