Conversion from 'Shape*' to non-scalar type 'Shape' requested

  1. JOZ

    JOZ 8

    I get this error and I dont know how to get rid of it...

    I declared the Shape struct

    typedef struct
    {
    int tShape;
    int centerX;
    int centerY;
    int sizeX;
    int sizeY;
    int color;
    int filled;
    } Shape;

    When I try to send a Shape struct called shape to a function i get the error...

    shp_draw(shape);

    The function im sending it to is:

    void shp_draw(Shape shape)
    {
    if(shape.tShape==(2))
    drawBox(shape);
    else if (shape.tShape==(1))
    drawEllipse(shape);
    else
    drawCircle(shape);
    }

    I dont know why im getting this error ive been trying to figure it out for an hour with no luck...
     
  2. jcsd
  3. I don't receive this error. Here is the .c file I compile with using gcc.
    How are you declaring your structure in the main() function?

    Code (Text):

    #include <stdio.h>

    typedef struct {
        int tShape;
        int centerX;
        int centerY;
        int sizeX;
        int sizeY;
        int color;
        int filled;
    } Shape;

    void shp_draw(Shape shape);

    int main(void) {

        Shape shape;
       
        shp_draw(shape);

        return 0;
    }

    void shp_draw(Shape shape) {
        if(shape.tShape==(2)) {
            //drawBox(shape);
            printf("DrawBox called.");
        } else if (shape.tShape==(1)) {
            //drawEllipse(shape);
            printf("DrawEllipse called.");
        } else {
            //drawCircle(shape);  
            printf("DrawCircle called.");
        }
    }
     
  4. DrGreg

    DrGreg 1,977
    Science Advisor
    Gold Member

    JOZ, how are the functions drawBox, drawEllipse, and drawCircle declared? Is that your problem?
     
  5. Mark44

    Staff: Mentor

    Emphasis added...
    It would be helpful if you told us which error you're getting.
     
  6. DrGreg

    DrGreg 1,977
    Science Advisor
    Gold Member

    That's what I thought, but I guess the answer is "Conversion from 'Shape*' to non-scalar type 'Shape' requested".
     
  7. JOZ

    JOZ 8

    The draw functions are

    static void drawCircle(Shape shape)
    {
    if(shape.filled==(1))
    {
    setfillstyle(SOLID_FILL, shape.color);
    fillellipse(shape.centerX, shape.centerY, shape.sizeX,shape.sizeX);
    }
    else
    {
    setcolor(shape.color);
    ellipse(shape.centerX, shape.centerY, 0, 360, shape.sizeX, shape.sizeX);
    }
    }

    They are all basicaly the same as that but im still bringing it in as Shape shape but thats the only line that i get an error with... and i move it arround the same heeps of times
     
  8. JOZ

    JOZ 8

    So ive gotten rid of the error i thaught i had tried this before without it working... but it works now

    void shp_draw(Shape *shape)
    {
    if(shape->tShape==(2))
    drawBox(shape);
    else if (shape->tShape==(1))
    drawEllipse(shape);
    else
    drawCircle(shape);
    }
     
  9. Seems to me you declared the shape variable as a pointer in the first place. You didn't include that key piece of information in your post, but I would think you've done this (hopefully you did allocate space for it with malloc!):

    Shape *shape = (Shape*)malloc(sizeof(Shape));

    rather than:

    Shape shape;

    The compiler tells you it's a pointer, so you must have declared it so.

    Of course, you could pass it to the function by dereferencing it:

    shp_draw(*shape);

    Though I don't see why you'd want to pass it by value anyway. You're probably right in using a pointer (passing by reference). Of course, that doesn't mean you have to declare the variable you're passing as a pointer. You could do something like this:

    Shape shape;
    shp_draw(&shape);

    That's assuming a signature of 'void shp_draw(Shape *shape)'.

    Do make sure you allocate the structure when you declare it, if you declare it as a pointer to Shape. And remember to free it when you're done.
     
  10. JOZ

    JOZ 8

    Yeah i did i forgot about that and i did have issues with memory allocation and you just solved my problem i was about to post, and yeah i do need to use it as a pointer it gets passed arround to heeps of fuctions

    Thanks
     
  11. JOZ

    JOZ 8

    So i have another problem now... i need to go through my list and find every time one bit of the data struct equals a value do something.. what i want to do it

    else if(key==('B'))
    {
    clearviewport();
    do{
    shape=lst_next(list);
    if(shape->tShape==(2))
    shp_draw(shape);
    }while(shape!=NULL);
    }

    so get the data from the list check is tShape is 2 if it is draw it if its not then check the next one untill it returns NULL

    the lst_next function i have is

    void *lst_next(List *lst)
    {
    if(lst->iterator != NULL)
    {
    lst->iterator = nd_getNext(lst->iterator);
    if(lst->iterator != NULL)
    return nd_getData(lst->iterator);
    }
    return NULL;
    }

    I got this function from my lecturer but when i try to use it like this is gets "error: invalid conversion from 'void*' to 'Shape*'" is this because the lst_next function is a void? but in the list.c file that i got its full of voids that return something? which i thaught is weird but i got them from the lecturer...
     
  12. Making lst_next return Shape* is probably just fine. Since you will only ever be interested in Shape* types, it's best to be specific with the type.

    void* isn't quite what you'd think at first glance. It basically means a pointer to "something" that isn't specified. You can cast a void* to a specific type, since there's not much you can do with whatever the void* points to unless you have a type.

    So void* is quite different from just void in the sense that void means nothing, whereas void* is a pointer to something not specified.

    A quick comment on lines such as this:

    else if(key==('B'))

    The parentheses around 'B' are quite unnecessary, and just clutter it up and make it harder to read. Also, the parentheses around the expression in the if statement make it look too much like a function. This is perhaps a matter of opinion, but I would format functions like this:

    some_function()

    But I would put a space after any statement that is followed by a parenthesized expression. I also tend to like to space out expressions a bit so things don't end up as a solid chunk of characters. Readability is very important. So in short, I suggest you format that line like this:

    else if (key == 'B')

    If nothing else, however, don't put the parentheses around 'B'.

    Lastly, please wrap your code in code tags so it will be formatted better. It's hard to read like that, and you lose your indentation. Just put [ code] at the start and [ /code] at the end of your block of code, removing the spaces I put in after the square bracket (which I added so the forum software won't think it's a real tag).
     
Know someone interested in this topic? Share this thead via email, Google+, Twitter, or Facebook

Have something to add?