【问题标题】:calling a class constructor present in a dll managed C++调用 dll 管理的 C++ 中存在的类构造函数
【发布时间】:2012-10-08 12:10:56
【问题描述】:

我在 DLL(它使用 /clr 运行时,ManagedC++)中创建了一个类,并在该类中定义了一个构造函数。代码如下:

//Following is defined in something.h//

namespace ABC
{
public ref Class XYZ
{
public: int a;
public: XYZ();
};

//In something.cpp, I've the below code to define the constructor of the class created//

#include something.h

namespace ABC
{
 XYZ::XYZ()
  {
     a = 100;
  }
}

上面的项目是内置到DLL中的

在另一个项目中,我尝试使用 XYZ 类,如下所示:

#include something.h
using namespace ABC;

//inside main, I've following code

{
ABC::XYZ^ prq = gcnew ABC:XYZ();
prq->a=200;

......
...
}

在此,我收到一条错误消息 -

unresolved token (06000001) ABC.XYZ::.ctor

您能帮忙解决一下这里的问题吗?

【问题讨论】:

    标签: dll managed-c++


    【解决方案1】:

    问题是链接器找不到构造函数的定义。它位于另一个 DLL 中。在托管项目中,您可以通过添加对程序集的引用来解决这个问题。右键单击您的项目、属性、通用属性、框架和引用。单击添加新引用按钮。如果项目位于同一解决方案中,请使用“项目”选项卡。否则为“浏览”选项卡。

    另请注意,您现在不再需要 .h 文件。声明是从程序集中的元数据中导入的。

    【讨论】:

    • 你是对的!正如你提到的,我在包含头文件时遇到了错误。我已经包含了命名空间。试图重新定义错误 XYZ()。但我不明白,包含头文件如何使其重新定义?
    猜你喜欢
    • 2011-09-14
    • 2018-05-25
    • 1970-01-01
    • 2012-09-28
    • 2020-11-28
    • 1970-01-01
    • 2011-12-07
    • 2019-03-14
    • 2012-05-12
    相关资源
    最近更新 更多