×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

copying std::string to CString

copying std::string to CString

copying std::string to CString

(OP)
I am having a difficult time moving data from std::string to a CString that is part of a structure.  Here is the essence:

CODE

typedef struct
{
   int        stream_number;
   CString    sfid_name;
   int        sifd_counter;
   CString    msg_prefix;
   int        length;
}  td_input_stream;
td_input_stream *p_stream_struct;
p_stream_struct = new( td_input_stream );
status = Get_Next_Definition( &expected, &t_string );
if( status )
{
   CString sfid_name( t_string.c_str());  // test code, is ok
   p_stream_struct->sfid_name( t_string.c_str() ); // desired code, NOT ok
   p_stream_struct->sfid_name = sfid_name;
}
The error for the NOT ok line of code is:  

Quote:

error C2064: term does not evaluate to a function taking 1 arguments
There is a clue that I don't know how to interpert. Upon hovering the cursor over sfid_name in the first line of test code
the displayed hint is: CString sfid_name
Upon hovering over the ->sfid_name the hint is: CString __unnamed_00a8_1::sfid_name
What do I need to change?


 

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 

RE: copying std::string to CString

(OP)
And as long as std::strings are on the menu, if I may be permitted: std::string named one_line contains:

Quote:

In Stream Begin
with no leading or trailing spaces.  The code contains:

CODE

descriptor = one_line.substr( 0, location );
if( descriptor.compare( "In Stream Begin" ) )[ ... ] ...
But the if statement does not reflect a true condition.  Why not?
 

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 

RE: copying std::string to CString

std::string::compare returns int value (0 on is equal to).

CODE

if (descriptor.compare("In Stream Begin")) [ // is NOT equal!

The OP:
May be you want

CODE

p_stream_struct->sfid_name = t_string.c_str();
?
Syntactically you try to call sfid_name as a function or a pointer to function.
  

RE: copying std::string to CString

(OP)
Your reply was helpful.  I am almost there but some troubles remain.  The application is in Visual Studio 2008, MFC, No ATL, No CLR.  I have some code to read text from a file. That code uses reads the text into an std::string. When an error is discovered I need to show it to a user with afxMessageBox(). After a bunch of permutations the code looks something like:

CODE

bool Get_Stuff( std::string *data_type, int *value )
{
...
// error display stuff
std::string temp2 = *data_type;
CString temp2( temp1.c_str() );
CString a_string;
a_string.Format( L"expected text is %s and more info", temp2 );
AfxMessageBox( a_string );
...
return status;

I am trying to get the string from *data_type into my error message for the user. The debugger shows that *data_type contains valid text.  What must I do to get it in the message box?

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 

RE: copying std::string to CString

Try this:

CODE

a_string.Format(L"... %s ...", (LPCTSTR)temp2);
May be it helps...

Thank God, I don't use MFC ;)
 

RE: copying std::string to CString

(OP)
For the record, I think I finally have a handle on this concept. (Please don't hammer me for typos and syntax, my code is at work and I cannot cut and paste.)

CODE

std::a_string = "a_string";
std::string *b_string = "b_string";

  // does not work
CString c_string.Format(L"a is %s b is %s", <and any form of cast or translation>);

CString d_string( a_string.c_str() );
CString e_string( b_string->c_str() );
CString f_string;  // don't format it yet, just declare

f_string.Format( L"a is %s b is %s", d_string, e_string );
When I need the contents of a std::string, or even a _bstr_t, in a CString, I must move it into a CString (completely, all by its self), then I can format into the target CString with the .Format() method.
I need to do this because the vendor starter application uses CString in the MFC project, and uses _bstr_t for calls to their application, and finally, the std function to read text files uses std::string.
I have had cases where trying to declare f_string (above) and formatting it in the same line of code just does not work while using two line of code does work.  I can now make it all work so I won't spend much time sweating it.  (All the places I need this are in the user part of the applications and time is not a problem.  The real time part does not need all these gyrations.)

Thank you again for all your responses.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close