如何解决我试图向我的班级添加一个新的模板辅助函数,但现在出现 LNK2001 错误如何解决?
我最近在 StackOverflow 上问了几个问题,看看我是否可以通过使用模板将一些功能合并为一个。这些问题是:
- Can these methods that convert safe arrays into std::list objects be turned into a template function?
- Can this template function be adapted to account for the following method?
我还有一个功能要尝试和更新,所以我想自己试一试。
这是要更新的函数:
void CMSATools::ConvertSAFEARRAY_DATE(SAFEARRAY* psaDates,MeetingDates& rMapMeetingDates)
{
DATE *pVals = nullptr;
HRESULT hr = SafeArrayAccessData(psaDates,(void**)&pVals); // direct access to SA memory
if (SUCCEEDED(hr))
{
long lowerBound,upperBound; // get array bounds
hr = SafeArrayGetLBound(psaDates,1,&lowerBound);
if (FAILED(hr))
throw _com_error(hr);
hr = SafeArrayGetUBound(psaDates,&upperBound);
if (FAILED(hr))
throw _com_error(hr);
long cnt_elements = upperBound - lowerBound + 1;
for (int i = 0; i < cnt_elements; ++i) // iterate through returned values
{
COleDateTime datNotAvailable(pVals[i]);
DWORD dwDatNotAvailable = EncodeMeetingDate(0,datNotAvailable);
rMapMeetingDates[dwDatNotAvailable] = datNotAvailable;
}
hr = SafeArrayUnaccessData(psaDates);
if (FAILED(hr))
throw _com_error(hr);
}
else
{
throw _com_error(hr);
}
hr = SafeArrayDestroy(psaDates);
if (FAILED(hr))
throw _com_error(hr);
}
MeetingDates
的定义如下:
using MeetingDates = std::map<DWORD,COleDateTime>;
所以我创建了这个辅助函数:
template<>
void CMSATools::to_push_back(const DATE& rItem,MeetingDates& rItems)
{
COleDateTime datNotAvailable(rItem);
DWORD dwDatNotAvailable = EncodeMeetingDate(0,datNotAvailable);
rItems[dwDatNotAvailable] = datNotAvailable;
}
我像这样调整了我的调用代码:
theApp.MSAToolsInterface().ConvertSAFEARRAY<DATE,MeetingDates>(psaDates,mapMeetingDates);
但是当我编译这个时,我现在得到一个错误:
5>PublishersDatabaseDlg.obj : error LNK2001: unresolved external symbol "public: static void __cdecl CMSATools::ConvertSAFEARRAY
我做错了什么?
到目前为止,当我进行 DATE
... 模板化调用时,我的代码无法编译:
template<typename to>
void CMSATools::to_clear(to& rItems)
{
rItems.clear();
}
template<typename from,typename to>
void CMSATools::to_push_back(const from& rItem,to& rItems)
{
rItems.push_back(rItem);
}
template<>
void CMSATools::to_clear(CStringArray& rItems)
{
rItems.RemoveAll();
}
template<>
void CMSATools::to_push_back(const BSTR& rItem,CStringArray& rItems)
{
rItems.Add(rItem);
}
template<>
void CMSATools::to_push_back(const DATE& rItem,datNotAvailable);
rItems[dwDatNotAvailable] = datNotAvailable;
}
template<typename from,typename to>
void CMSATools::ConvertSAFEARRAY(SAFEARRAY* psaItems,to& rItems)
{
from* pVals = nullptr;
HRESULT hr = SafeArrayAccessData(psaItems,upperBound; // get array bounds
hr = SafeArrayGetLBound(psaItems,&lowerBound);
if (FAILED(hr))
throw _com_error(hr);
hr = SafeArrayGetUBound(psaItems,&upperBound);
if (FAILED(hr))
throw _com_error(hr);
to_clear<to>(rItems);
long cnt_elements = upperBound - lowerBound + 1;
for (int i = 0; i < cnt_elements; ++i) // iterate through returned values
{
to_push_back<from,to>(pVals[i],rItems);
}
hr = SafeArrayUnaccessData(psaItems);
if (FAILED(hr))
throw _com_error(hr);
}
else
{
throw _com_error(hr);
}
hr = SafeArrayDestroy(psaItems);
if (FAILED(hr))
throw _com_error(hr);
}
我这是对类似标题问题的回答:
Error when pass std::map as template template argument
而且我认为它可能适用于我的情况,但我不确定如何实现它。如果确实是这个原因。
我的头类中有这个片段:
template<typename to>
static void to_clear(to& rItems);
template<typename from,typename to>
static void to_push_back(const from& rItem,to& rItems);
template<>
static void to_clear(CStringArray& rItems);
template<>
static void to_push_back(const BSTR& rItem,CStringArray& rItems);
template<>
static void to_push_back(const DATE& rItem,MeetingDates& rItems);
template<typename from,typename to>
static void ConvertSAFEARRAY(SAFEARRAY* psaItems,to& rItems);
static DWORD EncodeMeetingDate(int iMeetingType,COleDateTime datMeeting);
在头文件的顶部,我有:
#pragma once
#include "DemoPickerDlg.h"
#include <map>
#include <vector>
#ifdef _WIN64
#import "..\\..\\MSAToolsLibrary\\MSAToolsLibrary\\bin\\x64\\Release\\MSAToolsLibrary.tlb" raw_interfaces_only named_guids
#else
#import "..\\..\\MSAToolsLibrary\\MSAToolsLibrary\\bin\\x86\\Release\\MSAToolsLibrary.tlb" raw_interfaces_only named_guids
#endif
using MeetingDates = std::map<DWORD,COleDateTime>;
using ListDiscussionItems = std::list<MSAToolsLibrary::IDiscussionItemPtr>;
using ListStudentItems = std::list<MSAToolsLibrary::IStudentItemPtr>;
using ListDutyHistoryLookupItems = std::list<MSAToolsLibrary::IDutyAssignmentLookupPtr>;
解决方法
评论中有一个链接问题 (Why can templates only be implemented in the header file?) 提供给我。但答案本身并没有让我清楚。 它链接到的文章帮助我理解:
引用:
但是为了理解为什么事情是这样的,首先要接受以下事实:
- 模板不是类或函数。模板是编译器用来生成一系列类或函数的“模式”。
这让我很清楚。我将模式代码移动到标题中,现在可以正常编译。我发现这是最简单的解决方法。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。