| New Reply |
Conversion from 'Shape*' to non-scalar type 'Shape' requested |
Share Thread | Thread Tools |
| May22-11, 12:03 PM | #1 |
|
|
Conversion from 'Shape*' to non-scalar type 'Shape' requested
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... |
| May22-11, 01:16 PM | #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.");
}
}
|
| May22-11, 01:35 PM | #3 |
|
|
JOZ, how are the functions drawBox, drawEllipse, and drawCircle declared? Is that your problem?
|
| May22-11, 01:36 PM | #4 |
|
Mentor
|
Conversion from 'Shape*' to non-scalar type 'Shape' requested
Emphasis added...
|
| May22-11, 01:48 PM | #5 |
|
|
|
| May22-11, 05:40 PM | #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 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 |
| May22-11, 06:14 PM | #7 |
|
|
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); } |
| May22-11, 06:43 PM | #8 |
|
|
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. |
| May22-11, 07:16 PM | #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 |
| May22-11, 08:11 PM | #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 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... |
| May23-11, 02:28 AM | #11 |
|
|
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). |
| New Reply |
| Thread Tools | |
Similar Threads for: Conversion from 'Shape*' to non-scalar type 'Shape' requested
|
||||
| Thread | Forum | Replies | ||
| 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 & Comp Sci | 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 | ||