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


by JOZ
Tags: conversion, hape, nonscalar, requested, type
JOZ
JOZ is offline
#1
May22-11, 12:03 PM
P: 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...
Phys.Org News Partner Science news on Phys.org
SensaBubble: It's a bubble, but not as we know it (w/ video)
The hemihelix: Scientists discover a new shape using rubber bands (w/ video)
Microbes provide insights into evolution of human language
pheeesics
pheeesics is offline
#2
May22-11, 01:16 PM
P: 8
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?

#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.");
    } 
}
DrGreg
DrGreg is offline
#3
May22-11, 01:35 PM
Sci Advisor
PF Gold
DrGreg's Avatar
P: 1,809
JOZ, how are the functions drawBox, drawEllipse, and drawCircle declared? Is that your problem?

Mark44
Mark44 is online now
#4
May22-11, 01:36 PM
Mentor
P: 21,073

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


Emphasis added...
Quote Quote by JOZ View Post
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...
It would be helpful if you told us which error you're getting.
DrGreg
DrGreg is offline
#5
May22-11, 01:48 PM
Sci Advisor
PF Gold
DrGreg's Avatar
P: 1,809
Quote Quote by Mark44 View Post
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".
JOZ
JOZ is offline
#6
May22-11, 05:40 PM
P: 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
JOZ
JOZ is offline
#7
May22-11, 06:14 PM
P: 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);
}
Grep
Grep is offline
#8
May22-11, 06:43 PM
P: 296
Quote Quote by JOZ View Post
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);
}
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.
JOZ
JOZ is offline
#9
May22-11, 07:16 PM
P: 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
JOZ
JOZ is offline
#10
May22-11, 08:11 PM
P: 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...
Grep
Grep is offline
#11
May23-11, 02:28 AM
P: 296
Quote Quote by JOZ View Post
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).


Register to reply

Related Discussions
How to let the cold work shape memory alloy to acquire shape memory properties Materials & Chemical Engineering 2
error: conversion from `PriceFeed*' to non-scalar type `PriceFeed' requested Programming & Computer Science 2
what's differance between J-shape cure & S-shape curve Biology, Chemistry & Other Homework 1
Shape Shape-shifting strings... solids, liquids, and gasses Beyond the Standard Model 2