Thread: Error: explicit type cast cannot convert 'void* to 'int*'

  1. #1
    Registered User juschillin's Avatar
    Join Date
    Sep 2002
    Posts
    20

    Error: explicit type cast cannot convert 'void* to 'int*'

    I'm a newbie, so please bear with:

    Here's my problem and the line I get an error on:

    int *buf = GlobalAlloc(GPTR, sizeof(int) * count);

    when I compile the program that contains this line in C everything works fine. However when I rename my file to .cpp then I get the following error:

    error C2440: 'initializing' : cannot convert from 'void *' to 'int *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast


    I am missing something in there I know. Something about bout c++ requires explicit casts.

    What am I missing and why do I need it?

    Any help would be great!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    C++ forces you to consider typesafety more than C

    int *buf = (int*)GlobalAlloc(GPTR, sizeof(int) * count);

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    int *buf = (int*)GlobalAlloc(GPTR, sizeof(int) * count);
    That should do it

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    dang it fordy! you're fighting for that mod spot aren't you! just a half a second too late with my reply

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Quickest draw in the west

    Originally posted by FillYourBrain
    dang it fordy! you're fighting for that mod spot aren't you! just a half a second too late with my reply
    ::Blows smoke from the barrel of revolver::




  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Why are you using GlobalAlloc with a GPTR? This has none of the benefits of HANDLE based memory allocation and it has all of the problems of being non-standard. A simple "new" would get you the same results if I'm not mistaken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. Segmentation Fault?
    By John_L in forum C Programming
    Replies: 10
    Last Post: 10-02-2007, 08:37 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. type cast or conversion?
    By steviecrawf in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 03:08 PM