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
  • 115,525
  • 42
  • 225
  • 281
meWantToLearn
  • 1,609
  • 2
  • 22
  • 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
  • 39,598
  • 10
  • 94
  • 164
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
  • 227,678
  • 33
  • 352
  • 470
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
  • 265,791
  • 20
  • 170
  • 303
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
  • 242,813
  • 27
  • 432
  • 630