-1

I'm trying compile and run simple code presented below

#include <iostream>


class Date {
    int d,m,y;
    static Date default_date;
public:
    Date(int dd=0, int mm=0, int yy=0);
    static void set_default(int d, int m, int y);
};

Date::Date(int dd, int mm, int yy)
{
    d = dd ? dd : default_date.d;
    m = mm ? mm : default_date.m;
    y = yy ? yy : default_date.y;
}

void Date::set_default(int d, int m, int y)
{
    default_date = {d, m,y};
}

void f()
{
    Date::set_default(4,5,1945);
}

int main()
{
    f();
    return 0;
}

but I am getting a linker error:

`error: undefined reference to `Date::default_date'`

I am using QT creator with MinGW 4.8

Could you comment what is wrong ?

Leopoldo
  • 795
  • 2
  • 8
  • 23
  • 4
    You need a `Date Date::default_date(/*...*/);` in one of the translation units (cpp file). You have declared it, but not defined the `default_data`. [See this answer](http://stackoverflow.com/a/12574407/3747990). – Niall Nov 12 '14 at 11:49

1 Answers1

1

You need to define static members in implementation file. So corresponding to below declaration,

static Date default_date;

intialisation goes in implementation file

Date Date::default_date(12,12,2014);
ravi
  • 10,994
  • 1
  • 18
  • 36