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

  • Thread starter JOZ
  • Start date
  • #1
JOZ
8
0
I get this error and I don't 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 I am 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 don't know why I am getting this error I've been trying to figure it out for an hour with no luck...
 

Answers and Replies

  • #2
pheeesics
8
0
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:
#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.");
    } 
}
 
  • #3
DrGreg
Science Advisor
Gold Member
2,438
1,701
JOZ, how are the functions drawBox, drawEllipse, and drawCircle declared? Is that your problem?
 
  • #4
36,881
8,933
Emphasis added...
I get this error and I don't 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 I am 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 don't know why I am getting this error ive been trying to figure it out for an hour with no luck...

It would be helpful if you told us which error you're getting.
 
  • #5
DrGreg
Science Advisor
Gold Member
2,438
1,701
It would be helpful if you told us which error you're getting.
That's what I thought, but I guess the answer is "Conversion from 'Shape*' to non-scalar type 'Shape' requested".
 
  • #6
JOZ
8
0
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 I am still bringing it in as Shape shape but that's the only line that i get an error with... and i move it arround the same heeps of times
 
  • #7
JOZ
8
0
So I've 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);
}
 
  • #8
Grep
298
2
So I've 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);
}

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.
 
  • #9
JOZ
8
0
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
 
  • #10
JOZ
8
0
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 until 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...
 
  • #11
Grep
298
2
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...

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

Suggested for: Conversion from 'Shape*' to non-scalar type 'Shape' requested

Replies
0
Views
363
Replies
1
Views
561
  • Last Post
Replies
7
Views
594
  • Last Post
Replies
6
Views
518
  • Last Post
Replies
2
Views
343
Replies
1
Views
487
Replies
16
Views
885
Replies
10
Views
893
Top