错误:LNK2001:未解析的外部符号“private:static class” [英] Error: LNK2001: unresolved external symbol "private: static class

查看:581
本文介绍了错误:LNK2001:未解析的外部符号“private:static class”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个论坛包含很多这样的情况的例子,但是在我的例子中,静态变量被正确定义,但是我仍然得到这个错误。所以这个问题并不重复,以前的和以上的链接都没有回答这个问题。建议的 21个答案没有Simon给我的解决方案,请取消标记为重复。

This forum contains many examples of such situation, but in my case static variables are defined correctly, however I still get that error. So this issue is not duplicate of previous and above link does not answer the question. Suggested 21 answers post does not have solution Simon gave me here, please unmark this as "duplicate".

似乎我已经正确地声明了,请检查:

Seems I've declared all correctly, check this:

.h文件:

class ValueSetsModelsContainer : public QObject
{
  Q_OBJECT

public:
  static void DLLEXPORT loadAllergiesValueSets(MPTDatabase *db);
  static void DLLEXPORT loadProceduresValueSets(MPTDatabase *db);

  // Models access functions
  static QStandardItemModel *drugsModel();
  static QStandardItemModel *substanceModel();
  static QStandardItemModel *reactionsModel();

private:
  static QStandardItemModel *myDrugsModel, *mySubstanceModel, *myReactionsModel;
};

.cpp:

QStandardItemModel *ValueSetsModelsContainer::myDrugsModel = 0;
QStandardItemModel *ValueSetsModelsContainer::mySubstanceModel = 0;
QStandardItemModel *ValueSetsModelsContainer::myReactionsModel = 0;

QStandardItemModel *ValueSetsModelsContainer::drugsModel()
{
  return ValueSetsModelsContainer::myDrugsModel;
}

QStandardItemModel *ValueSetsModelsContainer::substanceModel()
{
  return ValueSetsModelsContainer::mySubstanceModel;
}

QStandardItemModel *ValueSetsModelsContainer::reactionsModel()
{
  return ValueSetsModelsContainer::myReactionsModel;
}

所以静态变量在cpp中定义,但是我仍然在另一个调用ValueSetsModelsContainer方法的模块:

So static variables are defined in cpp, however I still get linking error in another module which calls ValueSetsModelsContainer methods:



  • allergiesdialog.obj:-1:错误:LNK2001:未解析的外部符号
    private:static class QStandardItemModel *
    ValueSetsModelsContainer :: myDrugsModel
    (?myDrugsModel @ ValueSetsModelsContainer @@ 0PAVQStandardItemModel @@ A)

  • allergiesdialog.obj: -1:error:LNK2001:未解析的外部符号private:static class QStandardItemModel *

    ValueSetsModelsContainer :: mySubstanceModel

    (?mySubstanceModel @ ValueSetsModelsContainer @@ 0PAVQStandardItemModel @@ A) li>
  • allergiesdialog.obj:-1:error:LNK2001:未解析的外部符号private:static class QStandardItemModel *

    ValueSetsModelsContainer :: myReactionsModel

    ?myReactionsModel @ ValueSetsModelsContainer @@ 0PAVQStandardItemMode l @@ A)

问题可能在哪里?

推荐答案

从你的链接命令,原来你将对象链接到一个DLL中,而不是在第二个步骤中将DLL与你的最终二进制文件相链接。这可能是由您的项目中的子目录模板造成的设置。

From your link commands it turned out that you are linking together objects into a DLL and than in a second step link the DLL with your final binary. This might be caused by a subdirs template in your project settings.

每当您想要从外部获取DLL的方法时,您需要通过 __ declspec(dllexport)。我想这是在您的自定义预编译器常量 DLLEXPORT 中完成。

Whenever you want to have a method of a DLL available from outside, you need to make it available via __declspec( dllexport ). I guess this is done in your custom precompiler constant DLLEXPORT.

现在在.h文件中尝试这个: / p>

Now try this in your .h file:

static DLLEXPORT QStandardItemModel *drugsModel();
static DLLEXPORT QStandardItemModel *substanceModel();
static DLLEXPORT QStandardItemModel *reactionsModel();

使这些方法从DLL外部可用。

to make those methods available from outside the DLL.

顺便说一句:如果您只是将自己的项目中的东西连接起来,我不认为有一个中间的动态库(DLL) t需要使其可供其他人使用。考虑使用静态库,而不是在中设置 TEMPLATE = lib CONFIG + = staticlib .pro 其中ValueSetsModelsContainer所在的文件。但这是另一个主题和另一个问题。

By the way: I don't think it makes sense here to have an intermediate dynamic library (DLL) if your are just linking stuff from your own project and don't need to make it available to someone else. Consider using a static library instead by setting TEMPLATE = lib and CONFIG += staticlib in the .pro file where ValueSetsModelsContainer is in. But this is another topic and another question.

这篇关于错误:LNK2001:未解析的外部符号“private:static class”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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