Hello, I'm relatively new to C++. I've worked in C# and Java a fair amount, so I have a fairly decent understanding of programming concepts.
I've recently starting working on a mod for HL2 (not as anything commericial, just as a way for me to dive headfirst into c++ and start learning).
I've been playing for a few days, and I've run into an issue that I think (or, at least, hope) is language specific, and I don't quite grasp the symantics fully.
Anyway, I have a method of an object CBaseWeaponClip::SetMaxSize. It's code follows:
In another object I have a pointerCode:void CBaseWeaponClip::SetMaxSize( int maxSize ) { m_iClipSize = maxSize; // Clips hold at least a single bullet if ( m_iClipSize < 1 ) { m_iClipSize = 1; } if (m_iBulletsInClip > m_iClipSize) { m_iBulletsInClip = m_iClipSize; } }
I found out that the pointer changing was causing several bugs (and it didn't need to be changing), so for safety I relabled my pointer:Code:CBaseWeaponClip * c_cActiveClip
and changed other code using c_cActiveClip in that class appropriately.Code:CBaseWeaponClip * const c_cActiveClip
However, now when I run the game it crashes and tells me "The memory at location [memory location] could not be read."
I tracked the problem to this function. Specifically changing it to:
allows the game to load and run correctly (except weapon clips are no longer implemented properly), because SetMaxSize is a critical function. I moved my "return" statement down one line:Code:void CBaseWeaponClip::SetMaxSize( int maxSize ) { return; /*m_iClipSize = maxSize; // Clips hold at least a single bullet if ( m_iClipSize < 1 ) { m_iClipSize = 1; } if (m_iBulletsInClip > m_iClipSize) { m_iBulletsInClip = m_iClipSize; }*/ }
and I recieved the crash again.Code:void CBaseWeaponClip::SetMaxSize( int maxSize ) { m_iClipSize = maxSize; return; // Clips hold at least a single bullet /*if ( m_iClipSize < 1 ) { m_iClipSize = 1; } if (m_iBulletsInClip > m_iClipSize) { m_iBulletsInClip = m_iClipSize; }*/ }
I got rid of the maxSize variable (well, it was still being passed), and simply assigned 5 to m_iClipSize:
and I still recieved the crash.Code:void CBaseWeaponClip::SetMaxSize( int maxSize ) { m_iClipSize = 5; return; // Clips hold at least a single bullet /*if ( m_iClipSize < 1 ) { m_iClipSize = 1; } if (m_iBulletsInClip > m_iClipSize) { m_iBulletsInClip = m_iClipSize; }*/ }
I am assuming (quite possibly incorrectly) that relabling my pointer "const" somehow is restricting me from modifying variables like m_iClipSize. Is this the case?
If so, what is the proper fix? (It this was "mutable" was designed for?)
If not, what am I missing, or what information do I need to include?
As an afterthough it may be important: I am running Windows XP, with the Visual Studio 2003 compiler.
Thank you for your time, and I appreciate any responses.



LinkBack URL
About LinkBacks



CornedBee
Want to add some