[英]C++ - how to implement templated member function outside a templated class
[英]Exporting templated member function of a templated class in MSVC C++ DLL
我正在將PCL庫( https://github.com/PointCloudLibrary/pcl.git )編譯為DLL,以便在自己的項目中使用。 這是一個大量使用C ++模板的項目。 為了減少編譯時間,我使用PCL_NO_PRECOMPILE
設置為OFF
,這意味着該實現不在頭文件中。 我的應用程序只能使用在編譯PCL時已實例化的類和函數。
問題在於,模板類的模板成員函數未在MSVC DLL中導出。 我正在使用MS Visual Studio 2017社區版。 這是具體的問題。
我正在創建pcl::Hough3DGrouping
的子類,該子類在https://github.com/PointCloudLibrary/pcl/blob/master/recognition/include/pcl/recognition/cg/hough_3d.h定義。 感興趣的特定函數是在第510行的文件底部定義的受保護函數computeRf()
。當我查看DLL中的導出符號時,看不到computeRf()
。 因此,我無法從自定義代碼中使用它。
我假設因為它是模板化的,所以沒有在DLL中導出computeRf()
。 因此,我在https://github.com/PointCloudLibrary/pcl/blob/master/recognition/src/cg/hough_3d.cpp中嘗試了顯式實例化。 我特別添加了
template void pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB> >, pcl::PointCloud<pcl::ReferenceFrame>)
在https://github.com/PointCloudLibrary/pcl/blob/master/recognition/src/cg/hough_3d.cpp的第44行上。
編譯時出現錯誤:
E:\libs\pcl\src\recognition\src\cg\hough_3d.cpp(45): error C3190: 'void pcl::Hough3DGrouping<pcl::PointXYZRGB,pcl::PointXYZRGB,pcl::ReferenceFrame,pcl::ReferenceFrame>::computeRf(const boost::shared_ptr<const pcl::PointCloud<PointT>>,pcl::PointCloud<PointModelRfT>)' with the provided template arguments is not the explicit instantiation of any member function of 'pcl::Hough3DGrouping<pcl::PointXYZRGB,pcl::PointXYZRGB,pcl::ReferenceFrame,pcl::ReferenceFrame>'
with
[
PointT=pcl::PointXYZRGB,
PointModelRfT=pcl::ReferenceFrame
]
我的目標是在我的應用程序中創建pcl::Hough3DGrouping
的子類, pcl::Hough3DGrouping
調用基類的方法computeRf()
。 我唯一需要的實例是:
pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>
pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>
我應該如何對PCL源代碼進行最小的更改?
顯然,無法從DLL導出模板。
復制粘貼您嘗試導出的內容和簽名:
template<typename PointType, typename PointRfType> void
computeRf
(const boost::shared_ptr<const pcl::PointCloud<PointType> > &input, pcl::PointCloud<PointRfType> &rf)
template void
pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::
computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>
(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB> >, pcl::PointCloud<pcl::ReferenceFrame>)
我發現有所不同。 您試圖導出功能參數不是引用的內容。 模板類的模板方法具有引用函數參數。
編譯器說“這些不匹配”。 我會相信的。
引用參數,它將與該類中的方法匹配。
template void
pcl::Hough3DGrouping<pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame>::
computeRf<pcl::PointXYZRGB, pcl::ReferenceFrame>
(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB> >&, pcl::PointCloud<pcl::ReferenceFrame>&)
實例化行中有兩個錯誤:
Hough3DGrouping模板參數的順序錯誤,應該是
pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::PointXYZRGB, pcl::ReferenceFrame
並不是
pcl::PointXYZRGB, pcl::PointXYZRGB, pcl::ReferenceFrame, pcl::ReferenceFrame
您在參數列表中忘記了&
s:
computeRf(const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB>>&, pcl::PointCloud<pcl::ReferenceFrame>&)
模板參數computeRf
沒有必要的,因為演繹的作品。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.