C++ simple problem

This is a discussion on C++ simple problem within the C++ Programming forums, part of the General Programming Boards category; Hello.I'm a complete beginner with C++.I've been using the Devbloodshed IDE and running it on Windows XP. All I've really ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    5

    C++ simple problem

    Hello.I'm a complete beginner with C++.I've been using the Devbloodshed IDE and running it on Windows XP.
    All I've really run is simple apps with loops and arrrays etc.Anyway,I really want to learn how to
    create windows applications.This may sound like a simple error to some:
    int *buf = GlobalAlloc(GPTR, sizeof(int) * count);
    I receive an invalid convert from void to int error at this line.
    I've tried doing a simple cast such as
    int *buf =(int) GlobalAlloc(GPTR, sizeof(int) * count);
    and tried using dynamic and static casts all to no avail.
    What is interesting is this, GlobalAlloc function doesn't have a return,so how can it return anything other than void anyway?
    This isn't my application.The exe runs fine,but not when I try to run it in the Ide.
    Any help appreciated.I've listed the 1 compilation error in the code with a
    //my error comment.
    Code:
     
    #include <windows.h>
    #include "resource.h" 
    BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
     switch(Message)
     {
      case WM_INITDIALOG:
       // This is where we set up the dialog box, and initialise any default values
       SetDlgItemText(hwnd, IDC_TEXT, "This is a string");
       SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE);
      break;
      case WM_COMMAND:
       switch(LOWORD(wParam))
       {
        case IDC_ADD:
        {
         // When somebody clicks the Add button, first we get the number of
         // they entered
         BOOL bSuccess;
         int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
         if(bSuccess) 
         {
          // Then we get the string they entered
          // First we need to find out how long it is so that we can
          // allocate some memory
          int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
          if(len > 0)
          {
           // Now we allocate, and get the string into our buffer
           int i;
           char* buf;
           buf = (char*)GlobalAlloc(GPTR, len + 1);
           GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
           // Now we add the string to the list box however many times
           // the user asked us to.
           for(i = 0;i < nTimes; i++)
           {
            int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
            // Here we are associating the value nTimes with the item 
            // just for the heck of it, we'll use it to display later.
            // Normally you would put some more useful data here, such
            // as a pointer.
            SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
           }
           // Dont' forget to free the memory!
           GlobalFree((HANDLE)buf);
          }
          else 
          {
           MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
          }
         }
         else 
         {
          MessageBox(hwnd, "Couldn't translate that number :(", "Warning", MB_OK);
         }
        }
        break;
        case IDC_REMOVE:
        {
         // When the user clicks the Remove button, we first get the number
         // of selected items
         HWND hList = GetDlgItem(hwnd, IDC_LIST);
         int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
         if(count != LB_ERR)
         {
          if(count != 0)
          {
           // And then allocate room to store the list of selected items.
           int i;
     //my error   int *buf = GlobalAlloc(GPTR, sizeof(int) * count);
           SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf);
           
           // Now we loop through the list and remove each item that was
           // selected.  
           // WARNING!!!  
           // We loop backwards, because if we removed items
           // from top to bottom, it would change the indexes of the other
           // items!!!
           for(i = count - 1; i >= 0; i--)
           {
            SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
           }
           GlobalFree(buf);
          }
          else 
          {
           MessageBox(hwnd, "No items selected.", "Warning", MB_OK);
          }
         }
         else
         {
          MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
         }
        }
        break;
        case IDC_CLEAR:
         SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
        break;
        case IDC_LIST:
         switch(HIWORD(wParam))
         {
          case LBN_SELCHANGE:
          {
           // Get the number of items selected.
           HWND hList = GetDlgItem(hwnd, IDC_LIST);
           int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
           if(count != LB_ERR)
           {
            // We only want to continue if one and only one item is
            // selected.
            if(count == 1)
            {
             // Since we know ahead of time we're only getting one
             // index, there's no need to allocate an array.
             int index;
             int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index);
             if(err != LB_ERR)
             {
              // Get the data we associated with the item above
              // (the number of times it was added)
              int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);
              SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data, FALSE);
             }
             else 
             {
              MessageBox(hwnd, "Error getting selected item :(", "Warning", MB_OK);
             }
            }
            else 
            {
             // No items selected, or more than one
             // Either way, we aren't going to process this.
             SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-");
            }
           }
           else
           {
            MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
           }
          }
          break;
         }
        break;
       }
      break;
      case WM_CLOSE:
       EndDialog(hwnd, 0);
      break;
      default:
       return FALSE;
     }
     return TRUE;
    }
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
     LPSTR lpCmdLine, int nCmdShow)
    {
     return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    Someone else asked basically the same question not more than a couple of days ago.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    I think I found it.It was this:
    error converting void to char:

    FileSize = (LPSTR)GlobalAlloc(GHND,Buff);

    This would make sense for conversion to char but why
    wouldn't my
    int *buf =(int) GlobalAlloc(GPTR, sizeof(int) * count);
    work?
    I tried the heap functions also.Nothing worked.
    I'm a real Dweeb at C++.I need help.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    *sigh*

    Don't interchange HANDLEs and pointers so easily without knowing what you're doing.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    Should it have been this?
    int *buf =(*int) GlobalAlloc(GPTR, sizeof(int) * count);

    I'm lost.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Just use the C++ new operator when you can. GlobalAlloc() and LocalAlloc() are discouraged from being used by Microsoft. If you must use the Windows API, use HeapAlloc().

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    If you don't even know about casting, don't try learning the Win32 GUI API... Do you even know what that function is doing ? It does exactly the same thing as new, except that GlobalAlloc() called with the GPTR parameter will allocate memory from the heap and set it to 0. It looks like you're just following a tutorial and you're copying everything from it without having even a clue what everything means.

    For the record, it should be (int*) GlobalAlloc( ... ).

    Seriously, you really need to quit learning the Win32 API and learn about pointers and basic topics...

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    Hey listen buddy.I've been at C++ 2 days.If this is the attitude.
    Im going to get on this board then I won't be posting anymore.
    Right now I make my living as a php programmer and also
    have worked in vb.net.
    All I've seen here is a bunch of self rightous twerps not worthy of
    #@#@ on!!!

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Mmkay, but what part of his post was incorrect?

    If you don't know the basics of the language, why confuse yourself with advanced concepts that require your knowledge of the language to be pretty solid? That's all you're doing. You're getting confused with simple memory allocation and casting, and such, but you're working on the Windows API. You have to make sure you get the language down right because C++ is much less forgiving than php or VB, whether it's .NET or otherwise.

    Just calm down for a second, and vent your frustration in another manner. Maybe take a break, get a nice beverage, and then go back to the details of the language. When it makes sense, go back to the Windows API for a bit.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    903
    If you've been at C++ 2 days, you are clearly far from being ready learning the Win32 API, no matter what your programming background is. Don't try going too fast, it will do you absolutely no good.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    5
    HY Dynasty!You seem a bit arrogant.I've done Windows API programming in visual basic but it's limited.What I want to do is build 'user interface ' templates quickly in c++ so I can get down to the real programming,which is pure logic.
    Running loops or whatever.Connnecting to databases.
    I don't see anything wrong with trying to take apart ready made examples as I have.It's a good way to learn,but Dynasty detests such a 'learners' attitude.
    I don't think I'll be back on this board again.

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    903
    Dynasty ? You mean... Desolation ? Look, we're not putting you down. We're just telling you that you're shooting yourself in the foot by doing so. VB and PHP are nothing alike C++. PHP may have a similar syntax, but C++ is much harder than PHP and like McGyver said, "C++ is much less forgiving than php or VB, whether it's .NET or otherwise".

  13. #13
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40

    Thumbs up

    Spinal,
    One of the first things C++ throws at you are pointers and dereferencing.
    I'm currently coming up to speed on them myself. Here are a couple links.
    add "dereferencing" to your search inputs.

    http://www.cplusplus.com/doc/tutorial/pointers.html
    http://forums.devshed.com/
    _____________________
    PS - Does your VB go up to 11?
    Last edited by Dave++; 06-11-2007 at 02:52 PM. Reason: speeling

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by spinaltap View Post
    Hey listen buddy.I've been at C++ 2 days.If this is the attitude.
    Im going to get on this board then I won't be posting anymore.
    Right now I make my living as a php programmer and also
    have worked in vb.net.
    All I've seen here is a bunch of self rightous twerps not worthy of
    #@#@ on!!!
    *chews straw* boy, round these parts, we eat php programmers for breakfast.


    You were given good advice. You can either accept it and maybe learn something or you can choose to believe you know how to learn C++ better than those of us who've been through the process.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM

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