1

Need to log the content of buf using the LogMethod() below the problem is that LogMethos only accepts a "Const CString&"

char buf[1024];
strcpy(buf, cErrorMsg);

// need to pass to LogMethod "buf" how do i do that?
log.LogMethod(const CString &); 

Thans Rev

Reversed

5
  • 1
    what is CString? do you refer to ATL/MFC? In C++, the standard string is std::string. CString can be anything, for instance a class coded by a coworker in your code base. Commented Jan 14, 2010 at 15:46
  • //log.LogDebug(CString(buf)); //log.LogDebug(buf); //str = buf; //log.LogDebug(str); Neither of the code above worked... Commented Jan 14, 2010 at 18:27
  • Then the problem is somewhere else. Show us the code for LogMethod. Commented Jan 14, 2010 at 21:47
  • CString i use is from MFC class i´m using VS6 Commented Jan 15, 2010 at 12:11
  • You were right! the problem was in my LogMethod class Thanks all for your help Commented Jan 15, 2010 at 12:46

3 Answers 3

1

If you're talking about MFC CString, as far as I can tell, it should have a non-explicit constructor taking TCHAR const *. In other words, the following should work.

log.LogMethod(buf); 

If it doesn't, please post the error message.

Sign up to request clarification or add additional context in comments.

Comments

1
log.LogMethod(CString(buf));

This will avoid the problem where the compiler won't automatically create the CString object using the appropriate constructor since the argument is a reference (It would have if the argument was a "plain" CString).

4 Comments

It makes no difference whether the parameter is CString or CString const &, in both cases the conversion will be performed. In the latter case, a temporary will be created and bound to the reference.
That is true in the general conversion case, however for CString this isn't true because the constructor (that accepts a char*) has been declared explicit which, unless i'm mistaken, keeps the compiler from being able to automatically chose it for such conversions. That said it does work in VS6 (where it is not marked explicit) when the CString& is const, but it does not work in VS2005 Reason: cannot convert from 'const char [5]' to 'const CString' Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
I very much doubt that Microsoft would change the explicitness between versions, as it would break a lot of existing code. Besides, documentation (msdn.microsoft.com/en-us/library/cws1zdt8%28VS.80%29.aspx) clearly shows the constructor is not explicit. Are you sure you didn't try compiling as Unicode while still passing char const * as a parameter (which indeed would trigger the explicit non-TCHAR constructor)?
@avakar - I stand corrected. I had accepted the defaults for the project, it was making "const char*" unicode, and when changed to non-unicode it worked as you describe (compiling without error).
0
CString cs;
cs = buf;

log.LogMethod(cs)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.