Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying CString to CString

Status
Not open for further replies.

thebarslider

Programmer
Dec 21, 2001
80
GB
Dear Everyone,

I am attempting to copy a CString passed into a function into another CString. Here is my example code:

void BeginRecording::SetProgrammeName(CString xmlProgrammeName )
{

CString m_firstProgrammeName = xmlProgrammeName;

}

The program always breaks on the copy line and stops at this statement:

CStringData* GetData() const throw()
{
return( reinterpret_cast< CStringData* >( m_pszData )-1 );
}

atlsimpstr.h file.

I am unsure why this is happening as i have seen other example code where you can copy two CString's.

A forwarded thank you to anyone who can help.

Mark.
 
Mark, it would appear that the source of your error is in the code that calls BeginRecording::SetProgrammeName since there is nothing wrong with that code.

Try changing the function signature to this and see if you get a compile error:

Code:
void BeginRecording::SetProgrammeName(const CString& xmlProgrammeName )

-pete




 
Hey there again,

Thank you for your suggestion, i tried changing the function signature to what you said but still got the same error. Using the debugger i have found that the value passed to the function works is initialise to the correct string and the empty string (m_firstProgrammeName) is present as an empty string.

My function call comes from another object (Timer) and follows like this:

CString name;
name = ( xmlStartTimer.GetData() );
p_newBeginRecording->SetProgrammeName(name);

I dont see any errors in this call as i know the function is definately called.

I also know that the function always bombs out at the point i suggested before.

Do you know any other method i could utilise instead of using the = operator? I've tried searching MSDN but didnt find anything else under the CString section.

Thank you again for your generous help.
 
Hey Pete,

Just to let you know that i've found the error. I wasn't calling 'new' on my pointer to the class and therefore C++ wouldn't let me change the data in the class.

Thank you for you help.

Mark.
 
>>I wasn't calling 'new' on my pointer to the class and therefore C++ wouldn't let me change the data in the class.

Actually, when not &quot;newing&quot; a class, there isn't even an instance of the class, no data etc.
Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top