[英]Accessing an object from different class
我試圖定義和聲明一個類(ReorderInfo_DK),該類需要從我的主代碼“ FlashBack.cpp”中引用另一個舊對象(MrProt&rMrProt)。 但是,我不完全知道如何將對象從主文件傳輸到頭文件,因此,我收到了無法解決的錯誤:
z:\\ n4 \\ pkg \\ mrservers \\ mrimaging \\ seq \\ cestipat \\ ReorderInfo_DK.h(11):錯誤C2071:'ReorderInfo_DK :: rMrProt':非法存儲類z:\\ n4 \\ pkg \\ mrservers \\ mrimaging \\ seq \\ cestipat \\ ReorderInfo_DK.h(22):錯誤C2758:'ReorderInfo_DK :: rMrProt':必須在構造函數庫/成員初始化列表中初始化
在“ ReorderInfo_DK.h”內部:
# include "MrServers/MrProtSrv/MrProt/prot.h"
class ReorderInfo_DK {
extern MrProt &rMrProt;
long lREOIndex;
public:
ReorderInfo_DK ();
~ReorderInfo_DK ();
long getLinNo (long lREOIndex);
};
ReorderInfo_DK::ReorderInfo_DK(){
lREOIndex = 0;
}
ReorderInfo_DK::~ReorderInfo_DK(){
}
內部主要代碼“ FlashBack.h”中有一個前向聲明
// Forward declarations
class MrProt;
然后,主代碼中的所有函數都使用“ MrProt&rMrProt”作為參數,例如:
NLSStatus FlashBack::prepare (MrProt &rMrProt, SeqLim &rSeqLim, SeqExpo &rSeqExpo)
{
………..
}
我打算在此函數“准備”中從我的類“ ReorderInfo_DK”中調用對象。
我將不勝感激任何幫助。
此致Dushyant
如果MrProt
(變量)是某個位置的全局變量,則需要將extern
聲明放在類之外 ,否則它將被視為ReorderInfo_DK
類的ReorderInfo_DK
。
如果它不是全局變量,則首先需要刪除extern
關鍵字以將其聲明為類中的成員變量,然后將實際實例傳遞給ReorderInfo_DK
構造函數,並在構造函數的初始值設定項列表中進行分配:
class ReorderInfo_DK
{
MrProt& MrProt;
public:
ReorderInfo_DK(MrProt& mrprot)
: MrProt(mrprot)
{}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.