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

In summary, the author is trying to send a Shape struct to a function, but gets an error. The error he is getting is "Conversion from 'Shape*' to non-scalar type 'Shape' requested". When he tries to pass the structure by value, he gets an error about not being able to allocate space for it. When he declares it as a pointer to Shape, he gets the same error.
  • #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...
 
Technology news on Phys.org
  • #2
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
JOZ, how are the functions drawBox, drawEllipse, and drawCircle declared? Is that your problem?
 
  • #4
Emphasis added...
JOZ said:
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
Mark44 said:
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
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
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
JOZ said:
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
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
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
JOZ said:
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).
 

1. What does it mean to convert from 'Shape*' to non-scalar type 'Shape'?

Converting from 'Shape*' to non-scalar type 'Shape' means changing the data type of a variable or object from a pointer to a non-scalar type, specifically a shape object.

2. Why would I need to convert from 'Shape*' to non-scalar type 'Shape'?

You may need to convert from 'Shape*' to non-scalar type 'Shape' if you want to access the properties and methods of a shape object, or if you want to pass a shape object as an argument to a function.

3. How is the conversion from 'Shape*' to non-scalar type 'Shape' done?

The conversion from 'Shape*' to non-scalar type 'Shape' is done by dereferencing the pointer, which means accessing the value of the variable or object that the pointer is pointing to.

4. Can any type of shape be converted from 'Shape*' to non-scalar type 'Shape'?

Yes, any type of shape can be converted from 'Shape*' to non-scalar type 'Shape', as long as the shape object is properly defined and has the necessary properties and methods.

5. Are there any risks or considerations when converting from 'Shape*' to non-scalar type 'Shape'?

There may be risks involved in converting from 'Shape*' to non-scalar type 'Shape' if the shape object is not properly defined or if the pointer is pointing to an invalid or null object. It is important to ensure that the conversion is done correctly to avoid any errors or unexpected behavior in the program.

Similar threads

  • Programming and Computer Science
Replies
6
Views
834
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
896
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
864
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top