943,838 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1126
  • C++ RSS
Oct 16th, 2008
0

invalid conversion

Expand Post »
Hi, i'm havnig problem with calling CreateThread. The problem is that it gives me a conversion error from const void* to void*

C++ Syntax (Toggle Plain Text)
  1. DWORD dwThreadId;
  2. HANDLE myThread;
  3.  
  4. myThread = CreateThread(
  5. NULL,
  6. 0,
  7. MyThreadFunction,
  8. "HELLO",
  9. 0,
  10. &dwThreadId);

dwThreadId id is the problem, the function is supose to take a pointer to a DWORD.

Thanks for any help
Cgris
Similar Threads
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 16th, 2008
1

Re: invalid conversion

Post a complete program that exhibits the problem, please. Also post the exact error message rather than summarizing.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 16th, 2008
0

Re: invalid conversion

OK, by no means is this safe. It was a meer test to get me started with threads.

C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. DWORD WINAPI MyThreadFunction( LPVOID lpParam );
  5.  
  6. int main()
  7. {
  8. DWORD dwThreadId;
  9. HANDLE myThread;
  10.  
  11. myThread = CreateThread(
  12. NULL,
  13. 0,
  14. MyThreadFunction,
  15. "HELLO",
  16. 0,
  17. &dwThreadId);
  18.  
  19. while(1){
  20. Sleep(3);
  21. std::cout << "main";
  22. }
  23. std::cin.get();
  24. return 0;
  25. }
  26.  
  27. DWORD WINAPI MyThreadFunction( LPVOID lpParam )
  28. {
  29. std::cout << lpParam;
  30. while(1){
  31. Sleep(10);
  32. std::cout << "thread";
  33. }
  34. return 0;
  35. }

exact error: 17 invalid conversion from `const void*' to `void*'

Thanks
Last edited by Freaky_Chris; Oct 16th, 2008 at 4:53 pm.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 16th, 2008
1

Re: invalid conversion

What compiler are you using?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 16th, 2008
0

Re: invalid conversion

*cough*
Dev-Cpp
Last edited by Freaky_Chris; Oct 16th, 2008 at 5:02 pm.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 16th, 2008
2

Re: invalid conversion

I'd be willing to bet that it's the string literal causing you problems. Try this:
C++ Syntax (Toggle Plain Text)
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. DWORD WINAPI MyThreadFunction( LPVOID lpParam );
  5.  
  6. int main()
  7. {
  8. DWORD dwThreadId;
  9. HANDLE myThread;
  10. char *param = "HELLO";
  11.  
  12. myThread = CreateThread(
  13. NULL,
  14. 0,
  15. MyThreadFunction,
  16. param,
  17. 0,
  18. &dwThreadId);
  19.  
  20. while(1){
  21. Sleep(3);
  22. std::cout << "main";
  23. }
  24. std::cin.get();
  25. return 0;
  26. }
  27.  
  28. DWORD WINAPI MyThreadFunction( LPVOID lpParam )
  29. {
  30. std::cout << lpParam;
  31. while(1){
  32. Sleep(10);
  33. std::cout << "thread";
  34. }
  35. return 0;
  36. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 16th, 2008
1

Re: invalid conversion

*cough*
Dev-Cpp
THIS compiled ok on dev-cpp:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. DWORD dwThreadId;
  4. HANDLE myThread;
  5. void *ptr=NULL;
  6.  
  7. myThread = CreateThread(
  8. NULL,
  9. 0,
  10. MyThreadFunction,
  11. ptr,
  12. 0,
  13. &dwThreadId);
  14.  
  15. while(1){
  16. Sleep(3);
  17. std::cout << "main";
  18. }
  19. std::cin.get();
  20. return 0;
  21. }

notice the void* ptr; I suspect problem is in your "HELLO", not in dwThreadId

[edit]And Narue's code compiles on Dev [/edit]
Last edited by Sci@phy; Oct 16th, 2008 at 5:08 pm.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 16th, 2008
0

Re: invalid conversion

Ah that would make much more sense. i did wonder, thanks guys.

i feel like a fool now lol!

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 16th, 2008
0

Re: invalid conversion

Just to mention, you don't have to declare char ptr, you can simply cast string:
(LPVOID)"HELLO";
But, if you want your string to work inside function that you call, you have to typecast it back to (char*)
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 16th, 2008
0

Re: invalid conversion

Click to Expand / Collapse  Quote originally posted by Sci@phy ...
Just to mention, you don't have to declare char ptr, you can simply cast string:
(LPVOID)"HELLO";
But, if you want your string to work inside function that you call, you have to typecast it back to (char*)
Yes, im aware of both of these, it was just the mere fact i was paying way to much time listening to my god damn compiler / IDE and not enough looking at what i had actually written!

Thanks,
Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: rock paper scissor program
Next Thread in C++ Forum Timeline: Inserting a node on a Tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC