简体   繁体   中英

Unable to pass a reference of 'this' to a constructor of another class

We have a class CJ that upon it's creation, it creates a different class BA object to which it wants to pass a reference to itself like so:

BA:BA(const CJ& myCJRef);

The compiler always errors with:

error: uninitialized reference member BA::myCJRef

CJRef is defined in the BA class as class type CJ

Now amounts of & , * or neither does anything but cause the same error. Initializing is tough since the initialization of myCJRef requires passing two other classes but the it would point to the wrong object.

I'm a 'C' guy... this is confounding.

Thanks all!


Following the below answers I got to the point where I used this pointer to call methods in the CJ object, the real code follows:

InputPoolBufferAdapter::InputPoolBufferAdapter (CJpsMixerChannel& _CJ, int jitter, int maxBuffers, unsigned long maxBufferAge): myCJpsMixerChannel (_CJ)

{
        myCJpsMixerChannel = _CJ;

        myJitter = jitter;  // assuming jitter will be the number of floats

        myJitterCounter = 0;

        myMaxBuffers = maxBuffers;

        myMaxBufferAge = maxBufferAge;

        myPopulateMetadataRequests = 0;

        mySendDataReadyAlert = true;

        clearBufferAdapterThreshhold = CLEAR_BUFFER_ADAPTER_THRESHHOLD;
};    

void InputPoolBufferAdapter::returnDataBufferToPool (ChannelBuffer_t buf) 
{
    void CJpsMixerChannel::myCJpsMixerChannel->returnBufferToInputPool(ChannelBuffer_t *returnBuffer_p);
};

void InputPoolBufferAdapter::notifyDataAvailable ()
{
    void myCJpsMixerChannel.notifyDestinationsDataAvailable(void);
};

void InputPoolBufferAdapter::bufferAdapterError (int a)
{
    void &myCJpsMixerChannel.inputBufferAdapterError(int error_code);
};

I tried several things as you can see but it will not compile, the errors are:

InputPoolBufferAdapter.cpp: In member function 'virtual void InputPoolBufferAdapter::returnDataBufferToPool(ChannelBuffer_t)': InputPoolBufferAdapter.cpp:33:50: error: expected initializer before '->' token InputPoolBufferAdapter.cpp: In member function 'virtual void InputPoolBufferAdapter::notifyDataAvailable()': InputPoolBufferAdapter.cpp:38:32: error: expected initializer before '.' token InputPoolBufferAdapter.cpp: In member function 'virtual void InputPoolBufferAdapter::bufferAdapterError(int)': InputPoolBufferAdapter.cpp:43:32: error: expected initializer before '.' token

I am at a loss, any more ideas for this no longer 'confounded' but definitely 'befuddled' software guy (I can't believe after 25 years in 'C' embedded systems this has got me so screwed up!).

Thanks all

I need to guess a bit, but your class BA also has a member variable called myCJRef , right? Its type is const CJ& , just like the parameter. In that case, you need to do this:

BA::BA( const CJ& r ) : myCJRef( r )
{
  // note: myCJRef = r; would not work here.
}

because you need to initialize the member variable. The error does not refer to the parameter of your ctor.

Presumably BA has a non-static member const CJ& myCJRef . A reference type object cannot be left uninitialized, so you need to make sure this reference is initialized by the constructor's member initialization list. So the definition of the constructor will look like so:

BA::BA(const CJ& myCJRef)
  : myCJRef(myCJRef)
{
  // ...
}

Everything after the : is the member initialization list. In this case, it says initialize the member myCJRef with the argument myCJRef (it's okay that they have the same name).

You should post some code. Anyway, without using any C++11 syntax, this is the way I would go:

CJ::CJ() : BA(*this) { //CJ constructor here }

BA::BA(const CJ& myCJ) : myCJref(myCJ) { //BA constructor here }

Here i only wrote CJ and BA constructors. BA must have a data member const CJ& myCJref for this to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM