"void* is not a pointer-to-object type"

This is a discussion on "void* is not a pointer-to-object type" within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a program in which a knob drawn in an OpenGL window functions as a GUI widget. ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    "void* is not a pointer-to-object type"

    I'm trying to write a program in which a knob drawn in an OpenGL window functions as a GUI widget. Here's the class for the knob:
    Code:
    class gluisKnob
    {
    public:
    	double value, max, min, default, step, scrollStep;
    	
    	void setTarget (void*);
    };
    //  Function to set the target
    void gluisKnob::setTarget (void * TARGET)
    {
    	target = TARGET;
    }
    There are more functions and members, but this is the important bit.

    The knob is to turn when clicked and dragged, or when scrolled on, and changes the value of a variable that target points to.

    I have no trouble assigning the address of some other variable (int, float, whatever) to target but how do I use that to assign a value to the variable it points to?

    example:
    Code:
    gluisKnob knob;
    double pitch;
    knob.setTarget (&pitch);
    ...
    //  This part is during the program's main loop
    if (user_clicks_and_drags_the_knob() )
    {
        knob.target = knob's_new_value;  // This doesn't work
        *knob.target = knob's_new_value;  // This gives the error message in the thread title
        knob.*target = knob's_new_value  // This doesn't work either.
    }

    Thanks,
    Chris

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    what is target defined as?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    the '.' takes precedence over the '*'. So your problem is that you are trying to call a member function, value from a pointer. There are two ways to overcome this, the first is with parenthesis (*object).function(); The second is the shorthand version, object->function(); Take your pick as they are exactly the same.

    (edit)Woops, guess I misread. You need to dereferance target, but since target is a void pointer, it doesn't make sense to just dereferance that. Instead, cast it first the the object pointer you want, then dereferance it. IE *((item*)thing.target) = target.

    However, I would rather make a setter and getter for target then take this approach.
    Last edited by Cogman; 01-17-2009 at 07:16 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Quote Originally Posted by Cogman View Post
    (edit)Woops, guess I misread. You need to dereferance target, but since target is a void pointer, it doesn't make sense to just dereferance that. Instead, cast it first the the object pointer you want, then dereferance it. IE *((item*)thing.target) = target.
    Better yet, if you can, use a template class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Testing object pointer for null
    By VanJay011379 in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2002, 10:51 AM
  5. Void pointer. So, is this an actual object?
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2002, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21