Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1075261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:14:51+00:00 2026-05-16T21:14:51+00:00

how do I cast void *something to an object in standard C++? Specifically I

  • 0

how do I cast void *something to an object in standard C++?
Specifically I want want to cast void *userdata
to std::map<String, void*>

Is this possible? I am trying:

//void *user_data is a parameter of this function (callback)
std::map <String, void*> user_data_n; //this line is ok
user_data_n = static_cast<std::map<String, void *>>(*user_data); //I get the errors here.

ERRORs:

Spurious '>>' user '>' to terminate a template argument list
Expected '>' before '(' token
'void *' is not a pointer-to-object type

or is there a better way to carry information about the caller object and some other parameters I can pass to void *user_data?

UPDATE:

Ass suggested by @aaa carp I changed >> to > > and the first two errors were solved. The last is strange, Why do I get that kind of message when casting it here and not when putting that object when setting the callback?

std::map<String, void*> user_data_h;
user_data_h["Object"] = this; //this is a MainController object
user_data_h["h"] = h; //h was defined as int *h
createTrackbar("trackbar_H", winName, h, 255, trackbar_handler, &user_data_h);

where createTrackbar is defined as:

int createTrackbar( const string& trackbarname, const string& winname,
int* value, int count, TrackbarCallback onChange, void* userdata);

UPDATE2:

doing this solved my problem but following the same approach, why I still get error when trying to cast objects contained in my map object?

void trackbar_handler(int value, void *user_data){
std::map <String, void*> *user_data_map;
user_data_map = reinterpret_cast<std::map<String, void *> *>(user_data); //WORKED!! ;)

MainController *controller; //the same class type I put using "this" above
controller = reinterpret_cast<MainController *>( user_data_map["Object"]); //ERROR here

int *var = reinterpret_cast<int*> (user_data_map["h"]); //ERROR also here
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:14:52+00:00Added an answer on May 16, 2026 at 9:14 pm

    When you’re casting from a void *, your result will be a pointer too. So the map declaration should be:

    std::map <String, void*> *user_data_n;
    

    Second, you should use reinterpret_cast for such (potentially dangerous) casts:

    user_data_n = reinterpret_cast<std::map<String, void *> *>(user_data);
    

    Update:

    As others suggested, you could simply use a static_cast as well.

    Why do I get that kind of message when casting it here and not when putting that object when setting the callback?

    Any pointer can be implicitly converted to void *, but when converting it back to a pointer of some specific type, you need an explicit cast.

    why I still get error when trying to cast objects contained in my map object?

    As already mentioned in the comments, you need to dereference the pointer before using the map object. You might want to define a reference instead to make things easier:

    std::map <String, void*> &user_data_map =
        *(static_cast<std::map<String, void *> *>(user_data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do something like this: class foo { virtual void bool operator==(foo
I'm trying to perform the following cast private void MyMethod(object myObject) { if(myObject is
I have something like this going on in my Java program: void f(Object o)
How to cast a pointer to void object to class object?
I want to reinterpret cast a function pointer into a void* variable. The type
CAST(IH.InvoiceDate AS date) AS InvoiceDate this returns 2012-03-16 is it possible to format this
I'm trying to figure out a way to automatically cast something to an Action
I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
OK so I'm looking a some code which looks roughly like this: void DoSomething(object
Prob something im overlooking but this problem has annoyed me greatly. im trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.