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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • philwozza

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

    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(v oid* param) {
    if (!Started) {
    Param = param;
    if (ThreadHandle =
    (HANDLE)_begint hreadex(NULL,0, ThreadFunction, this,0,&ThreadI D))
    {
    if (Detached)
    {
    CloseHandle(Thr eadHandle);
    }
    }
    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(v oid*)'

    Under VC++ 8.0 the code works fine. Can anyone suggest a resolution.

  • cbmanica@gmail.com

    #2
    Re: invalid conversion from `const void*' to `void*'


    philwozza wrote:
    [color=blue]
    >
    > 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(v oid*)'
    >
    > Under VC++ 8.0 the code works fine.[/color]

    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.

    Comment

    • Ivan Vecerina

      #3
      Re: invalid conversion from `const void*' to `void*'

      "philwozza" <phil_w@uk-businessdirecto ry.co.uk> wrote in message
      news:1147472275 .126677.208950@ g10g2000cwb.goo glegroups.com.. .
      ....
      : 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(v oid*)'
      :
      : 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*)"h i");
      With a C++ cast, you'd write:
      Start( const_cast<char *>("hi") );


      --
      http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


      Comment

      • philwozza

        #4
        Re: invalid conversion from `const void*' to `void*'

        thanks for cleaning that up.

        Comment

        Working...