在 dll 中定义模板类的排序方法(使用 std::sort 进行排序)并从另一个项目调用它

发布于 2024-10-01 06:20:53 字数 1921 浏览 4 评论 0原文

抱歉我的英语不好。我有 2 个项目。项目1是一个MFC dll,包含类CMyContainer、类CEmployee。项目2是我的主要项目。在项目 2 中,我创建了一个 CEmployee 类型的 CMyContainer 实例。现在我想对容器进行排序,但出现错误

error LNK2019:无法解析的外部符号“bool __cdecl MyComparer(class CEmployee *,class CEmployee *)”(?MyComparer@@YA_NPAVCEmployee@@0@Z) 中引用function "public: void __thiscall CMyContainer::sortContainer(void)" (?sortContainer@?$CMyContainer@VCEmployee@@@@QAEXXZ)"

如何解决此问题?

// 项目 1 中的文件 MyContainer.h

#include <vector>

template <class T> 
class _declspec(dllexport) CMyContainer
{
public:
    CMyContainer(void);
    ~CMyContainer(void);
        ...

    void sortContainer(); 
private:
    std::vector<T*> items;  
    typename std::vector<T*>::iterator it;
};
template <class T> void CMyContainer<T>::sortContainer()
{
    typedef bool (*comparer_t)(T*,T*);
    comparer_t cmp = &MyComparer;
    std::sort(items.begin(), items.end(), cmp);
}


//File Employee.h in project 1
#include "MyContainer.h"


class _declspec(dllexport) CEmployee
{
public:
    CEmployee(void);
    ~CEmployee(void);
        void setEmployeeCode(CString);
        CString getEmployeeCode();
        friend bool MyComparer(CEmployee*, CEmployee*);
private:
        CString m_szEmployeeCode;
}

//File Employee.cpp in project 1
void CEmployee::setEmployeeCode(CString val){
    m_szEmployeeCode= val;
}


CString CEmployee::getEmployeeCode(){

    return  m_szEmployeeCode;
}
bool MyComparer(CEmployee*pEmp1, CEmployee*pEmp2)
{
    return (pEmp1->getEmployeeCode().Compare(pEmp2->getEmployeeCode())<0);
}
//File main.cpp in project 2
#include <..\Models\MyContainer.h>
#include <..\Models\Employee.h>
...
CMyContainer<CEmployee> *pListEmployee;
... // insert into pListEmployee
// sort pListEmployee
pListEmployee.sortContainer();//-> This command cause error

Sorry for my bad English. I have 2 projects. Project 1 is a MFC dll that contains class CMyContainer, class CEmployee. Project 2 is my main project. In project 2, I created an instance of CMyContainer of type CEmployee. Now I want to sort the container but I got an error

"error LNK2019: unresolved external symbol "bool __cdecl MyComparer(class CEmployee *,class CEmployee *)" (?MyComparer@@YA_NPAVCEmployee@@0@Z) referenced in function "public: void __thiscall CMyContainer<class CEmployee>::sortContainer(void)" (?sortContainer@?$CMyContainer@VCEmployee@@@@QAEXXZ)"

How can I fix this problem?

// file MyContainer.h in project 1

#include <vector>

template <class T> 
class _declspec(dllexport) CMyContainer
{
public:
    CMyContainer(void);
    ~CMyContainer(void);
        ...

    void sortContainer(); 
private:
    std::vector<T*> items;  
    typename std::vector<T*>::iterator it;
};
template <class T> void CMyContainer<T>::sortContainer()
{
    typedef bool (*comparer_t)(T*,T*);
    comparer_t cmp = &MyComparer;
    std::sort(items.begin(), items.end(), cmp);
}


//File Employee.h in project 1
#include "MyContainer.h"


class _declspec(dllexport) CEmployee
{
public:
    CEmployee(void);
    ~CEmployee(void);
        void setEmployeeCode(CString);
        CString getEmployeeCode();
        friend bool MyComparer(CEmployee*, CEmployee*);
private:
        CString m_szEmployeeCode;
}

//File Employee.cpp in project 1
void CEmployee::setEmployeeCode(CString val){
    m_szEmployeeCode= val;
}


CString CEmployee::getEmployeeCode(){

    return  m_szEmployeeCode;
}
bool MyComparer(CEmployee*pEmp1, CEmployee*pEmp2)
{
    return (pEmp1->getEmployeeCode().Compare(pEmp2->getEmployeeCode())<0);
}
//File main.cpp in project 2
#include <..\Models\MyContainer.h>
#include <..\Models\Employee.h>
...
CMyContainer<CEmployee> *pListEmployee;
... // insert into pListEmployee
// sort pListEmployee
pListEmployee.sortContainer();//-> This command cause error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ懵逼小萝莉 2024-10-08 06:20:53

尝试使用 _declspec(dllexport) 从 .dll 导出 MyComparer

Try to export MyComparer from the .dll with _declspec(dllexport)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文