problem with strcpy

Started by
5 comments, last by Rydis 20 years, 4 months ago
i dunno but i always seem to have trouble with this and can never figure it out. LPCTSTR *ClassName; void WinMainF::SetClassName(LPCTSTR *Name) { strcpy(ClassName, Name); } error C2664: ''strcpy'' : cannot convert parameter 1 from ''const char ** '' to ''char *'' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Advertisement
The secret is in the macro/typedef name.

LPCTSTR is already a pointer, so the correct definition is:

LPCTSTR ClassName;

(same for the other definition)

The error does actually say that, but it might not be obvious.


[edited by - JuNC on November 30, 2003 2:52:31 PM]
still get this error

error C2664: 'strcpy' : cannot convert parameter 1 from 'const char *' to 'char *'


[edited by - Rydis on November 30, 2003 3:35:24 PM]

From MAPINLS.H in visual studio:

...
typedef const TCHAR FAR * LPCTSTR;
...

From MSDN:
The const keyword modifies the type of a type declaration or the type of a function parameter, preventing the value from varying.

Perhaps you could try:
LPTSTR ?

Floru
yes that works but would it work if i pass it to the CreateWindow Function which takes a LPCTSTR?
Yes, you can happily do a char* -> const char* conversion, you don''t lose any of the qualifiers (in fact you gain). A const char* -> char* is invalid because it implies that you change an immutable string into a mutable string (well array really but lets not quibble). So if you do this you need to be explicit and add a cast.
alright thanks.

This topic is closed to new replies.

Advertisement