在另一个类中初始化类的对象

时间:2014-03-07 10:39:40

标签: c++

我有一个继承自MySeqBuildBlockModule的课程public SeqBuildBlock。  除了构造函数和析构函数之外,此类MySeqBuildBlockModule还有一个方法:prep

class MySeqBuildBlockModule: public SeqBuildBlock
{
    friend class SeqBuildBlockIRns;
    public:
        MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In)    // more arguements in this constructor of derived class
        : SeqBuildBlock (pSBBList0)
        {
            TI1 = TI1_In;  //in us
            TI2 = TI2_In;  //in us
            pSBBList2 = pSBBList0;
            //SeqBuildBlockIRns myIRns_3(pSBBList2);        //Defined in libSBB.h
        }
        //~MySeqBuildBlockModule(){}
        virtual bool prep (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo);
        virtual bool run  (MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo, sSLICE_POS* pSLC);  
    protected:

    private:
        long TI1;  //in us
        long TI2;  //in us
        SBBList* pSBBList2;
        SeqBuildBlockIRns myIRns_3(pSBBList2);                                      // Line 106
};

bool MySeqBuildBlockModule::prep(MrProt* pMrProt, SeqLim* pSeqLim, SeqExpo* pSeqExpo)
{
    NLS_STATUS    lStatus;
    double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();                      //Line 113

    // Now we prepare:
    lStatus = pSBBList->prepSBBAll(pMrProt, pSeqLim, pSeqExpo, &dEnergyAllSBBs_DK); // Line 116
    if (lStatus ) {
        cout << "DK: An error has occurred while preparing SBBs"<< endl;
    }
    return lStatus;
}

我想要实现第三方库中定义的类的对象“myIRns_3

SeqBuildBlockIRns myIRns_3(pSBBList2);

并希望从prep函数访问它:

double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();

我尝试在私有部分或构造函数中实例化以下内容;但是没有任何成功:

SeqBuildBlockIRns myIRns_3(pSBBList2);
遇到

错误

当我尝试在构造函数中执行此操作时,出现以下错误:

MySBBModule.h(113) : error C2065: 'myIRns_3' : undeclared identifier
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union

当我尝试在私有部分执行此操作时,我收到以下错误:

MySBBModule.h(106) : error C2061: syntax error : identifier 'pSBBList2'
MySBBModule.h(113) : error C2228: left of '.getEnergyPerRequest' must have class/struct/union type
MySBBModule.h(116) : error C2065: 'pSBBList' : undeclared identifier
MySBBModule.h(116) : error C2227: left of '->prepSBBAll' must point to class/struct/union

PS:我在发布的代码中标记了行号

任何帮助将不胜感激。 问候, DK

1 个答案:

答案 0 :(得分:0)

您的问题是以下声明了一个功能

SeqBuildBlockIRns myIRns_3(pSBBList2);

您要做的是宣布成员:

SeqBuildBlockIRns myIRns_3;

然后在构造函数中使用参数pSBBList2

构造它
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In)    // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0),
  myIRns_3(pSBBList2)  // <- construct
{ }