Initializing object of a class inside another class

I have a class MySeqBuildBlockModule that I am inheriting from: public SeqBuildBlock.
Other than constructor and destructor, this class MySeqBuildBlockModule has a method: prep.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
};


1
2
3
4
5
6
7
8
9
10
11
12
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;
}


I would have like to intiantiate an object "myIRns_3" of a class defined in third party library

SeqBuildBlockIRns myIRns_3(pSBBList2);

and would like to access it from the prep function as:
double dEnergyAllSBBs_DK = myIRns_3.getEnergyPerRequest();

I tried to instantiate following in either private section or in constructor; but w/o any success:
SeqBuildBlockIRns myIRns_3(pSBBList2);

ERRORS encountered:

When I tried to do it inside the constructor, I get the following errors:

1
2
3
4
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


When I tried to do it in private section, I get the following errors:

1
2
3
4
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: I have marked the line no in the code that I am posting.

Any help would be appreciated.
Regards,
DK
Last edited on
what errors did you get?
Hi Jaybob66,

Thanks for your response.

I have included error messages in my original post. Please have a look.

I have also marked line number where error exists.

Regards,
DK
This line looks wrong to me:

18
19
20
	private:
	// ...
		SeqBuildBlockIRns myIRns_3(pSBBList2);										// Line 106 


You can't invoke the constructor like that in a class definition. You need to simply declare the data member here, and then in the constructor(s) use the initialisation list to specify the argument that gets passed to the SeqBuildBlockIRns constructor.

Also, what on earth do you mean by:

 
		// NOT really part of private 


?
Last edited on
Hi MikeyBoy,

Thanks for your response.

Can you please suggest me where should I declare?

SeqBuildBlockIRns myIRns_3(pSBBList2);


Sorry for putting comment :

// NOT really part of private

This comment was for SeqBuildBlockIRns myIRns_3(pSBBList2); But, since I kept moving this declaration, that comment was mistakenly left there.
Thanks everyone,

I solved this problem as:

1) by declaring in private section:

SeqBuildBlockIRns myIRns_3;
rather than
SeqBuildBlockIRns myIRns_3(pSBBList2);

2) then modified it in the constructor with the argument pSBBList2:

1
2
3
4
MySeqBuildBlockModule (SBBList* pSBBList0, long TI1_In, long TI2_In)    // more arguements in this constructor of derived class
: SeqBuildBlock (pSBBList0),
  myIRns_3(pSBBList0)  // <- construct
{ }
Last edited on
Topic archived. No new replies allowed.