c ++复制结构未解析的链接 [英] c++ copy struct unresolved link

查看:27
本文介绍了c ++复制结构未解析的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C++ 编写日期包装类

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

我想将 tm 结构复制到另一个结构,但它抛出未解析的外部链接

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

错误 2 错误 LNK2001: 未解析的外部符号 "public: static struct tm * DateUtils::generateDateTimeStruct" (?generateDateTimeStruct@DateUtils@@2PAUtm@@A)

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;

}

推荐答案

您需要在类声明之外(在某些 .cpp 文件中)定义此成员:

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:

在类声明中:

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

但是类外的定义:

int DateUtils::getTodaysDate() { ... }
void DateUtils::generateDateTimeStruct() { ... }

这篇关于c ++复制结构未解析的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆