[英]No Suitable Conversion Function from LoadTask::MasterFilePtr to MasterFile * exists
我是C ++的新手,在理解某些轉換行為時遇到了一些麻煩。
在LoadTask.h
,輸入define MasterFilePtr
:
typedef std::shared_ptr<MasterFile> MasterFilePtr;
然后,我初始化masterFile
變量:
MasterFilePtr masterFile;
后來,在LoadTask.cpp
我通過masterFile
作為參數傳遞給函數:
dataLoader.SetMasterFile( masterFile );
該函數定義為:
void SetMasterFile( MasterFile * pMasterFile ) { m_pMasterFile = pMasterFile; };
雖然masterFile
會導致問題,但我收到一個錯誤:
不存在從
LoadTask::MasterFilePtr
到MasterFile *
合適轉換函數MasterFile *
我認為typedef
將MasterFilePtr
設置為與MasterFile *
等效,但是事實並非如此。
此外,我可以通過嘗試克服錯誤:
dataLoader.SetMasterFile( &*masterFile );
但是,這感覺非常錯誤,因此有人可以解釋這里發生了什么嗎?
LoadTask::MasterFilePtr
是std::shared_ptr<MasterFile>
的別名。 您不能將shared_ptr
傳遞給需要原始指針的函數-沒有定義隱式轉換。 為了從shared_ptr
提取原始指針,您需要使用get()
方法或使用發現的技巧。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.