Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链接器错误:DLL和继承_C++_Inheritance_Virtual_Dllexport - Fatal编程技术网

C++ 链接器错误:DLL和继承

C++ 链接器错误:DLL和继承,c++,inheritance,virtual,dllexport,C++,Inheritance,Virtual,Dllexport,我正在寻找错误的来源,因为几个小时没有成功。我的项目由两个子项目组成。第一个是dll,第二个是应用程序(exe)。 我简化了原始代码,它是dll的一部分: #ifndef blub_base #define blub_base #include "SomeInterface.hpp" #include "Object.hpp" #include <map> namespace a { namespace b { class __dec

我正在寻找错误的来源,因为几个小时没有成功。我的项目由两个子项目组成。第一个是dll,第二个是应用程序(exe)。 我简化了原始代码,它是dll的一部分:

#ifndef blub_base
#define blub_base

#include "SomeInterface.hpp"
#include "Object.hpp"
#include <map>

namespace a
{
    namespace b
    {       
        class __declspec(dllexport) CBase : public CSomeInterface
        {
        protected:
            CBase() {}


        public:

            CBase(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
            ~CBase() { /* concrete implementation in cpp */ }

            bool initialize() { /* concrete implementation in cpp */ }
            bool shutdown() { /* concrete implementation in cpp */ }

            void foo() { /* concrete implementation in cpp */ }
            virtual void blubblub(Object* f_object_p) { /* concrete implementation in cpp */ }
        protected:
            bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
            bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
            void blub3(Object* f_object_p) const  { /* concrete implementation in cpp */ }
        };
    }
}
#endif




#ifndef blub
#define blub

#include "Base.hpp"

namespace a
{
    namespace b
    {
        class __declspec(dllexport) CChild : public CBase
        {
        private:
            CChild() {}

        public:
            CChild(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
            /// deconstructor
            ~CChild() { /* concrete implementation in cpp */ }

            void blubblub(Object* f_object_p) override { /* concrete implementation in cpp */ }

        protected:
            bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
            bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
            void blub3(Object* f_object_p) const override { /* concrete implementation in cpp */ }
        };
    }
}
#endif
\ifndef blub#u base
#定义blub_基
#包括“SomeInterface.hpp”
#包括“Object.hpp”
#包括
名称空间a
{
命名空间b
{       
类declspec(dllexport)CBase:公共CSomeInterface
{
受保护的:
CBase(){}
公众:
CBase(somedifferentitobject*f_object_p){/*cpp中的具体实现*/}
~CBase(){/*在cpp*/}中的具体实现
bool initialize(){/*在cpp*/}中的具体实现
bool shutdown(){/*在cpp*/}中的具体实现
void foo(){/*cpp*/}中的具体实现
虚拟void blubblub(Object*f_Object_p){/*在cpp*/}中的具体实现
受保护的:
bool blub1(Object*f_Object_p,std::map&f_something_rmap)const{/*在cpp*/}中的具体实现
bool blub2(Object*f_Object_p,std::map&f_something_rmap)const{/*在cpp*/}中的具体实现
void blub3(Object*f_Object_p)const{/*在cpp*/}中的具体实现
};
}
}
#恩迪夫
#ifndef blub
#定义blub
#包括“Base.hpp”
名称空间a
{
命名空间b
{
类declspec(dllexport)儿童:公共数据库
{
私人:
CChild(){}
公众:
CChild(somedifferentitobject*f_object_p){/*cpp中的具体实现*/}
///解构器
~CChild(){/*在cpp中的具体实现*/}
void blubblub(Object*f_Object_p)重写{/*cpp*/}
受保护的:
bool blub1(Object*f_Object_p,std::map&f_something_rmap)const override{/*cpp中的具体实现*/}
bool blub2(Object*f_Object_p,std::map&f_something_rmap)const override{/*在cpp*/}中的具体实现
void blub3(Object*f_Object_p)const override{/*cpp*/}中的具体实现
};
}
}
#恩迪夫
如果我试图在我的应用程序中实例化CChild对象,我会得到CChild类所有函数的链接器错误:

错误75错误LNK2001:未解析的外部符号“public:virtual void\uu thiscall a::b::CChild::blubblublub(类a::b::Object*)”(?blubblub@CChild@b@a@@UAEXPAVObject@23@@Z) application.obj应用程序 错误74错误LNK2019:未解析的外部符号“public:virtual\u thiscall a::b::CChild::~CChild(void)”(??1CChild@b@a@@UAE@XZ)函数“public:virtual void*u thiscall a::b::CChild::`vector deleting destructor'(unsigned int)”(?)_ECChild@b@a@@UAEPAXI@Z)Application.obj应用程序 错误73错误LNK2019:未解析的外部符号“public:u thiscall a::b::CChild::CChild(类a::b::SomeDifferentObject*)”(??0CChild@b@a@@QAE@PAVSomeDifferentObject@12@@Z)在函数\u main Application.obj应用程序中引用 错误77错误LNK2001:未解析的外部符号“受保护:虚拟布尔调用a::b::CChild::blub1(类对象*,类std::映射,类std::分配器>>&)常量”(?blub1@CChild@b@a@@分子束外延_NPAVObject@23@AAV$map@KIU?$less@K@性病病毒$allocator@U?$pair@$$CBKI@std@@@2@@@std@@@Z)Application.obj应用程序 错误76错误LNK2001:未解析的外部符号“受保护:虚拟布尔调用a::b::CChild::blub2(类对象*,类std::映射,类std::分配器>>&)常量”(?blub2@CChild@b@a@@分子束外延_NPAVObject@23@AAV$map@KIU?$less@K@性病病毒$allocator@U?$pair@$$CBKI@std@@@2@@@std@@@Z)Application.obj应用程序 错误78错误LNK2001:未解析的外部符号“受保护的:虚拟无效”\uu thiscall a::b::CChild::blub3(类a::b::Object*)const”(?blub3@CChild@b@a@@MBEXPAVObject@23@@Z) Application.obj应用程序

我使用的是Visual Studio,所有cpp文件都在项目中(已检查多次)。每个功能的实现,例如:。 CChild::CChild(somedifferentitobject*f_object_p):CBase(f_object_p) { }

似乎找不到相关的cpp文件

非常感谢你的帮助

亲切问候,,
Bobby

它不起作用,因为类总是导出的。它们需要由dll项目导出,并由使用该dll的项目导入

要解决此问题,请添加头文件,例如ab_dll.h,然后在其中:

#ifdef AB_DLL_EXPORT 
    #define AB_DLL_API __declspec(dllexport)
#else
    #define AB_DLL_API __declspec(dllimport)
#endif
然后在类中使用该宏:

class AB_DLL_API CBase : public CSomeInterface
{
//...
};

class AB_DLL_API CChild : public CBase
{
//...
};

另外,在VS dll项目中,在
PreprocessorDefinitions
中添加
AB\u dll\u EXPORT
,以便导出类。这样,如果在项目中定义了
AB\u DLL\u EXPORT
,则将导出类,否则将导入类

谢谢你的回复。我已经实现了您的建议,但这并没有带来解决方案:(在查看MyDll.vcxproj后,我发现Child.hpp和Child.cpp不包括在项目组中。奇怪的是,它们列在MyDll.vcxproj.filters中。手动添加这两个文件后,链接器错误消失了。这似乎是一个VS错误。