1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Using Visual studio 2008 and error C2664 C2440

Discussion in 'C++' started by soong, Sep 30, 2008.

  1. soong

    soong New Member

    Joined:
    Sep 30, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I've been doing basic programming with Java, and have recently started to familiarize with c++ so forgive me if this is a really noob question

    I'm using Microsoft Visual Studio 2008 and looking at code examples from the internet

    when I try to compile some code I usually get the errors


    for the line
    ofn.lpstrFilter = "Text Files (*.txt)\0*.ufc\0All Files (*.*)\0*.*\0";
    error C2440: '=' : cannot convert from 'const char [56]' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    which i can use the TEXT casting thing to overcome
    and


    for the line

    HWND hwnd = CreateWindow(sClassName,(LPCWSTR)TEXT(sAppName),WS_OVERLAPPED | WS_MINIMIZEBOX |WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_DLGFRAME | WS_BORDER, CW_USEDEFAULT,CW_USEDEFAULT,275,137,0,0,hinst,0);
    error C2440: '=' : cannot convert from 'const char [56]' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    which i have not been able to solve yet.

    I was wondering why do I have to edit code which should already be working? I thought perhaps its to with using C code and adding it to a C++ project, but these examples were taken from C++ programs.

    Thx
    soong
     
  2. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    There's a lot of casting involved in Windows programming, due to the generic nature of the interfaces. As long as you're certain the data pointed to is correct, there's nothing wrong with a cast. There's a lot less casting nowadays than there used to be, but casting from different string types to each other is still unfortunately quite common.

    Don't double cast though, as in (LPCWSTR)TEXT(szAppName). You should only ever need one cast.

    You don't say where the code comes from, but my guess is that it's either from older Windows programs where the rules aren't as strict or they're from non-Windows programs.
     
  3. soong

    soong New Member

    Joined:
    Sep 30, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    thx for reply, and sorry for my late reply

    and yeah i'm assuming that the code is pretty old.
     

Share This Page