string::operand+ usage problem (with WIN32 data types)

This is a discussion on string::operand+ usage problem (with WIN32 data types) within the C++ Programming forums, part of the General Programming Boards category; I'm using DevC++ as compiler and this is a Win32 program... Code: LRESULT CALLBACK Efendi(HWND anaPen, UINT msj,WPARAM wParam, LPARAM ...

  1. #1
    Registered User flashbaz-pi's Avatar
    Join Date
    Nov 2008
    Posts
    7

    string::operand+ usage problem (with WIN32 data types)

    I'm using DevC++ as compiler and this is a Win32 program...

    Code:
    LRESULT CALLBACK Efendi(HWND anaPen, UINT msj,WPARAM wParam, LPARAM lParam){
            int metKrkSys;
            LPTSTR metMetin;
            HWND hMet;
            hMet = GetDlgItem( anaPen, id_met);
            switch (msj) {
                   case WM_COMMAND:
                        metKrkSys = GetWindowTextLength(hMet);
                        GetWindowText( hMet, metMetin, metKrkSys+1);
                        switch (LOWORD(wParam)){
                               case id_rakam0:
                                    metMetin = metMetin + "abc";
                                    MessageBox( anaPen, metMetin, "KarakterSayısı", MB_OK);
                                    break;            
                        }
                        break;
                   case WM_CLOSE:
                        DestroyWindow(anaPen);
                        break;
                   case WM_DESTROY:
                        PostQuitMessage(0);
                        break;
                   default:
                   return DefWindowProc(anaPen, msj, wParam, lParam);      
            }
            return 0;
    }
    anaPen is my main window.
    hMet (id_met) is an edit control.
    metMetin is the text in this edit control.
    id_rakam0 is a button's id.

    This program wants to do this: getting the text in the edit control, adding it "abc" string then showing in a MessageBox...

    When I compile:

    39 D:\Denemeler\c++\win32_winprog\HesapMakinesi\main. cpp invalid operands of types `TCHAR*' and `const char[3]' to binary `operator+'

    So what's the problem exactly and what should I do?
    Last edited by flashbaz-pi; 11-01-2008 at 05:29 AM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    metMetin is a pointer to TCHAR. So you cannot use the + operator for it. You should use the strcat() to append "abc" to metMetin.
    In other words, I believe metMetin is a c-string, not a C++ string, so no overloaded + operator is provided.

  3. #3
    Registered User flashbaz-pi's Avatar
    Join Date
    Nov 2008
    Posts
    7

    Smile

    Oh man, thanks a lot! This is exactly what I need.

    Note: To use StrCat() function, program needs shlwapi.h header. That means U must add

    Code:
    #include <shlwapi.h>
    to beginning of code.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by C_ntua View Post
    In other words, I believe metMetin is a c-string, not a C++ string, so no overloaded + operator is provided.
    You do need to believe. It is a C-style string (TCHAR*).
    However, it being a pointer, you should not append any characters to it.
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    Instead, the best way to do this would be to make it std::string instead. Then you can use operator + as normal.
    But then again, since metMetin is undefined from the beginning (if it is a pointer), plus contains nothing, it would be better to do only an assign and not an addition with operator +. metMetin is also a local variable so there is no chance it could contain anything else.

    And strcat is a C API, not a Windows API, so shlwapi.h is unnecessary. You should be including string.h to use it. But then again, you should not use it at all, since this is C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User flashbaz-pi's Avatar
    Join Date
    Nov 2008
    Posts
    7
    strcat() is a C API, right. But StrCat() is a Win API. I tried <string.h> and compiler asked for StrCat(). If you use StrCat() in code, compiler needs <shlwapi.h> ! Using StrCat or strcat, it doesn't matter. Bot of them works!

    I read the link above. But I couldn't understand how to convert a TCHAR* pointer to a std::string!??

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Code:
    TCHAR* str;
    std::string s = str;
    Assuming, you are not using unicode.
    And StrCat is Windows API, so it is tied to Windows. When strcat in the C runtime libraries does the exact same thing, this is what you should have been using instead. But regardless, in C++, you should be working with std::string and not char*, wchar_t* or TCHAR*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User flashbaz-pi's Avatar
    Join Date
    Nov 2008
    Posts
    7
    Here is the problem: I convert TCHAR to std::string. That's ok. But when I

    Code:
    std::string metStr = metMetin;
    metStr += "abc";
    MessageBox ( anaPen, metStr, "Hey", MB_OK);
    Compiler: cannot convert `std::string' to `const CHAR*' for argument `2' to `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'

    ??

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Code:
    std::string metStr = metMetin;
    metStr += "abc";
    MessageBox(anaPen, metStr.c_str(), "Hey", MB_OK);
    Due to conversion operators being unpredictable in the current standard due to implicit conversions, std::string does not include one for const char*, so you have to call the c_str() member function.
    The next standard should bring explicit conversion operators to solve this problem, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    >> The next standard should bring explicit conversion operators to solve this problem, though.
    I have not heard this. If it's more than a rumor, please show me.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    The next standard should allow the explicit keyword to be used in conversion operators, thus creating explicit conversion operators:
    http://en.wikipedia.org/wiki/C&#37;2B%2B...sion_operators
    If they decide to implement such operators in the classes remains to be seen, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Is TCHAR* the same as char*? Wouldn't the .c_str() return a char*, but the MessageBox wouldn't require a TCHAR*. I don't believe there is a problem but just to make sure.

    Also, just to mention that you could use strcat(), but you would probably have to copy metMetin contains to a strlen(netMetin+4) buffer and the append "abc" there. But of course this is C++ so a std::string would be much more easy

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    TCHAR* is either defined as char* or wchar_t* depending if the UNICODE macro is defined. Visual Studio provides project settings for this.
    std::string will only work if working with non-unicode, as TCHAR* will then be defined as char*, thus std::string will be able to accept a char* string and MessageBox will be able to take a const char* string (since c_str() returns const char*).
    If working with unicode, std::wstring should be used instead.
    Unfortunately, there are no "TCHAR" string in the library, but you could easily define one yourself:

    typedef std::basic_string<TCHAR> tstring;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User flashbaz-pi's Avatar
    Join Date
    Nov 2008
    Posts
    7
    Here is the bombbb! Program crashed!

    Code:
    std::string metStr = metMetin;
    Dev++ compiled this line, but program crached! I'm sure the problem is here. When I push the button which identified as "id_rakam0" the program is crashing.

    But that's not last! I want this metMetin as an integer also. Is there any function to do this? Of course must be... But I couldn't find
    Last edited by flashbaz-pi; 11-02-2008 at 01:01 PM.

  14. #14
    Registered User flashbaz-pi's Avatar
    Join Date
    Nov 2008
    Posts
    7
    Here is the bombbb! Program crashed!

    Code:
    std::string metStr = metMetin;
    DevC++ compiled this line, but program crashed! I'm sure the problem is here. When I push the button which identified as "id_rakam0" the program is crashing.

    But that's not last! I want this metMetin as an integer also. Is there any function to do this? Of course must be... But I couldn't find
    Last edited by flashbaz-pi; 11-02-2008 at 10:28 AM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by flashbaz-pi View Post
    Here is the bombbb! Program crashed!

    Code:
    std::string metStr = metMetin;
    DevC++ compiled this line, but program crashed! I'm sure the problem is here. When I push the button which identified as "id_rakam0" the program is crashing.

    But that's not last! I want this metMetin as an integer also. Is there any function to do this? Of course must be... But I couldn't find
    Of course the problem is not there. Unless you're claiming that metMetin is not a null-terminated array of characters? Who knows? (Hint: not us.) EDIT: Actually, reading this again, you've bollixed the call to GetWindowText; so that is what is crashing when you try to write to New Jersey, or wherever metMetin happens to be pointing. You must have actual space on the other end of the pointer metMetin (via malloc/new and friends) before calling the function.

    As to the other, you should look into strtol. (Again, you'd have to call it on the .c_str().)
    Last edited by tabstop; 11-02-2008 at 10:48 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Server recv data problem! Please help!
    By hyaku_ in forum Networking/Device Communication
    Replies: 15
    Last Post: 01-28-2005, 01:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21