Visual studio 2005 typecast problem with legacy code

Posted on 2007-08-01
Last Modified: 2013-12-14
I'm trying to get some old visual studio 6 code to compile in visual studio 2005. I'm getting the following compile time error:

"2>c:\documents and settings\user\desktop\xlistctrl_demo\xlistctrl\xtrace.h(74) : error C2440: '=' : cannot convert from 'const wchar_t *' to 'LPTSTR' "

This is the offending function:
            if (!XTRACE_SHOW_FULLPATH)
                  cp = _tcsrchr(m_file, _T('\\'));
                  if (cp)

Please advise
Question by:sternocera
    LVL 45

    Accepted Solution


    Hi sternocera,

    I'm not sure if the failure is due to the 'const' modifier or the recast from wchar_t* to LPTSTR.  I would suspect that if the problem were with 'const' that it would have already been a problem so let's focus on the recast.

    Compilers differ greatly in their behavior regarding pointer types.  Some flag errors when trying to set (or pass) a char* when the destination type is unsigned char*, others think it just fine.  The other extreme is the old C compiler on the DEC TRU64 systems which allowed you to set an integer variable to a pointer value (and vice versa) because they were both 32 bit.

    In your case you've got a pointer to a wide char (16-bit) and a pointer to a char (8-bit).  Check to see if there is a compiler option to be more forgiving of this.  If not, a quick change to the source code to perform a recast should do fine.

      cp = (wchar_t*) _tcsrchr (m_file, _T('\\'));

    Good Luck,
    LVL 86

    Assisted Solution

    Either declare the variable as 'LPCTSTR' (which might not be applicable, since it is incremented) or use a 'const_cast()', i.e.

                      cp = const_cast<LPTSTR>(_tcsrchr(m_file, _T('\\')));


    Author Comment

    Thanks. That worked.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now