invalid conversion from `const void*' to `void*'

Discussion in 'C++' started by philwozza, May 12, 2006.

  1. philwozza

    philwozza Guest

    Im trying to implement a THREAD class that encapsulates a posix thread.
    Here is an outline of my THREAD class.

    class THREAD {

    public:
    // returns 1 if thread sucessfully started
    int Start(void* = NULL);

    // other public functions

    protected:
    virtual void* Run(void*);

    };

    int THREAD::Start(void* param) {
    if (!Started) {
    Param = param;
    if (ThreadHandle =
    (HANDLE)_beginthreadex(NULL,0,ThreadFunction,this,0,&ThreadID))
    {
    if (Detached)
    {
    CloseHandle(ThreadHandle);
    }
    }
    Started = TRUE;

    }
    return Started;

    }

    Once the thread is started it basically calls Run() inside the
    ThreadFunction.

    I have a class called HELLO that inherits from the THREAD class that
    simply prints "hello" to the screen.

    class HELLO : public THREAD {

    protected:
    virtual void* Run(void*);
    };


    void* HELLO::Run(void* param) {
    char* message = param;
    for (int i=0;i<11;i++)
    {
    printf("%s\n", message);
    }
    return NULL;
    }

    Problem: when i create a new instance of HELLO and attempt to call
    Start("hi") i get the following error under gcc 3.3.5

    error: invalid conversion from `const void*' to `void*'
    error: initializing argument 1 of `int THREAD::Start(void*)'

    Under VC++ 8.0 the code works fine. Can anyone suggest a resolution.
     
    philwozza, May 12, 2006
    #1
    1. Advertising

  2. philwozza

    Guest

    philwozza wrote:

    >
    > Problem: when i create a new instance of HELLO and attempt to call
    > Start("hi") i get the following error under gcc 3.3.5
    >
    > error: invalid conversion from `const void*' to `void*'
    > error: initializing argument 1 of `int THREAD::Start(void*)'
    >
    > Under VC++ 8.0 the code works fine.


    In C++, string literals are of type const char [], so gcc is correct
    and the M$ product is technically wrong (as usual). You can either
    turn "hi" into a modifiable array of characters,

    char foo[]="hi";
    instance.Start( foo );

    or, since you do not actually modify the characters in the strings you
    pass, cast away the const:

    instance.Start( const_cast<char*>("hi") );

    , although this approach is a pretty sure path to tears down the road.
    I recommend not doing it.
     
    , May 12, 2006
    #2
    1. Advertising

  3. "philwozza" <> wrote in message
    news:...
    ....
    : int Start(void* = NULL);
    ....
    : Problem: when i create a new instance of HELLO and attempt to call
    : Start("hi") i get the following error under gcc 3.3.5
    :
    : error: invalid conversion from `const void*' to `void*'
    : error: initializing argument 1 of `int THREAD::Start(void*)'
    :
    : Under VC++ 8.0 the code works fine. Can anyone suggest a resolution.
    VC++ is wrong.
    A string literal has type char const[N],
    which implicitly get converted to char const*.
    The latter can implicitly be converted to a void const*.

    So an explicit cast is needed.
    IMO the easiest in such a context is to use a C-style cast:
    Start((void*)"hi");
    With a C++ cast, you'd write:
    Start( const_cast<char*>("hi") );


    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
     
    Ivan Vecerina, May 12, 2006
    #3
  4. philwozza

    philwozza Guest

    thanks for cleaning that up.
     
    philwozza, May 13, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    580
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    828
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    864
    S.Tobias
    Jul 22, 2005
  4. Javier
    Replies:
    2
    Views:
    603
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,158
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page