Reputation: 29
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
Upvotes: 1
Views: 133
Reputation: 2793
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.
Upvotes: 1
Reputation: 56863
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.
Upvotes: 2
Reputation: 110658
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).
Upvotes: 1