473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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.

May 12 '06 #1
3 30717

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 '06 #2
"philwozza" <ph****@uk-businessdirectory.co.uk> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.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(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
May 12 '06 #3
thanks for cleaning that up.

May 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
6
by: Thomas Barth | last post by:
Hi, I'm new to windows programming and still reading a book about windows-programming with C++. I copied the following code from the book into my ide (Eclipse/CDT) to comprehend the code, but two...
5
by: spoilsport | last post by:
Ive got to write a multi-function program and I'm plugging in the functions but I keep getting this error from line 40. Im new to this and cant find an answer anywhere. Sam #include...
11
by: Martin Jørgensen | last post by:
Hi, I'm using this alloc_mem-function: - - - - - - - - - - - - - - - - - - - - - - - - void *alloc_mem (size_t num_elems, size_t elem_size, char *filename, int line, size_t *total_mem) {
2
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
3
by: fazulu deen | last post by:
Hi all, For the following code : file_ptr = fopen("pass_fail.txt", "a"); // error line 393 fdisplay(file_ptr, "Test Passed"); fclose(file_ptr);
1
by: Nozdormu | last post by:
Hi guys, I have a quick question. The language I'm using is C. The question I'm tackling: Write a program that (1) defines a 1D array with ten int elements, and (2) sets the values of the...
6
bajajv
by: bajajv | last post by:
Hi, My code is - void func1(void* arg) { printf("In child thread.\n"); } int main()
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.