0

I am working on a date wrapper class in c++

I want to copy the tm structure to another structure, but it throws unresolved external link

Error 2 error LNK2001: unresolved external symbol "public: static struct tm * DateUtils::generateDateTimeStruct" (?generateDateTimeStruct@DateUtils@@2PAUtm@@A)

 class DateUtils
{
public:

    DateUtils()
    {

    }

    static int getTodaysDate();
    static tm * myDateTime;
    static void generateDateTimeStruct();

};

tm* DateUtils::myDateTime = NULL;

int DateUtils::getTodaysDate()
{
   // If i comment the calling, it does not throws an error
    generateDateTimeStruct();
    return DateUtils::myDateTime->tm_hour;
}
static void generateDateTimeStruct(){
        time_t now = time(0);
        static tm s;
        now = time(NULL);
        localtime_s(&s, &now);

        DateUtils::myDateTime = &s;

}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
meWantToLearn
  • 1,691
  • 2
  • 25
  • 45

4 Answers4

1

You need to define this member outside the class declaration (in some .cpp file):

tm* DateUtils::myDateTime = NULL;

Note that prefixing with the name of the class is used while defining the other members as well:

In the class declaration:

static int getTodaysDate();
static void generateDateTimeStruct();

but the definition outside the class:

int DateUtils::getTodaysDate() { ... }
void DateUtils::generateDateTimeStruct() { ... }
LihO
  • 41,190
  • 11
  • 99
  • 167
0

As myDateTime has been declared to have static storage, you need to assign some memory to it.

The normal thing to do is to define it in exactly one compilation unit; typically in a source file:

tm* Wrapper::myDateTime = NULL;
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

You have to define the static data member. It was only declared in the class definition but not defined outside the class. In some code module write

tm * Wrapper::myDateTime;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You declare a member function, but define a non-member function with the same name. Change the definition to

static void DateUtils::generateDateTimeStruct(){
    // your code here
}

Alternatively, don't define a class at all - it seems just to be somewhere to put static functions, in which case a namespace would be more appropriate.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644