View Full Version : Compile issues
dukebound85
Nov 28, 2006, 07:15 PM
Hi all, I can not seem to get this to compile. Any ideas on what could be tripping it up? This is a .h file that I am trying to create on my own
struct point
{
double x;
double y;
};
struct line
{
struct point p1;
struct point p2;
};
double length(line *1); //function prototype
double distance(point *p1, point *p2); //function prototype
Thanks for any help you can offer
it says there is a syntax error before the a token on my function prototype lines
iMeowbot
Nov 28, 2006, 07:34 PM
double length(struct line *1);
…and so on. Also the "*1" there looks like a typo.
dukebound85
Nov 28, 2006, 07:39 PM
double length(struct line *1);
…and so on. Also the "*1" there looks like a typo.
Thanks however I was told to use these function prototypes exactly. I had to define 2 structures, one that is point which has x and y and one that is line which contains 2 points.
Thanks for the help nonetheless!
iMeowbot
Nov 28, 2006, 07:43 PM
Thanks however I was told to use these function prototypes exactly.
Then the instructions have a problem. You won't be able to use those function prototypes without the struct keyword, unless you also add typedefs (http://en.wikipedia.org/wiki/Typedef).
Also, *1 is plain old wrong, a variable name can't be numeric.
Edit: One thing concerns me here, is that the header file you posted at the top of the thread would not run into this issue if it was supposed to be C++ (it does have the problems you reported as C, though). What language are you supposed to be learning here?
dukebound85
Nov 28, 2006, 07:54 PM
Then the instructions have a problem. You won't be able to use those function prototypes without the struct keyword, unless you also add typedefs (http://en.wikipedia.org/wiki/Typedef).
Also, *1 is plain old wrong, a variable name can't be numeric.
cool thanks, typedef worked. Also I tried *l as in "ell" rather than the number and it worked too. They look so similar lol
I have another question if you would be so kind. When I coompile my c file that makes use of the h file, I have a fuction that attempts to calculate the x coordinate difference of two points.
for example
double deltax=*p1.x-*p2.x;
however, the compiler returns the message "request for member a in something not a structure or union"
But isn't it though, since p1 is a point which in turn has x and y components in the point structure?
Once again, you help is grealty appreciated
iMeowbot
Nov 28, 2006, 09:07 PM
I'm kind of guessing without seeing the rest of the code, but the *p1.x probably wants to be either (*p1).x or the more conventional p1->x
The parens are necessary in the first form of a pointer into a struct, because . has higher precedence than * . (compiler doesn't know what to make of the * until too late).
dukebound85
Nov 29, 2006, 01:35 PM
Thanks that was the issue.
Got another question after playing around with it last night and this morning.
in my function,
double length(line *l)
{
double value;
value=distance((*l).(*p1), (*l).(*p2));
return value;
}
the compiler gets hung up on the line value=distance line
it says there is a syntax error before the a token.
am i correct in thinking (*l).(*p1) will point to point one on the first line entered and (*l).(*p2) will point to the second point of the first line? Then the distance fucntion would manipulate the points?
Thanks for the insight you have provided
pilotError
Nov 29, 2006, 01:42 PM
Try...
double length(line *l)
{
return distance( l->p1, l->p2 );
}
dukebound85
Nov 29, 2006, 01:51 PM
cool thanks, I'll try that when I get back to my computer
just wondering though, would that be equivalent to
(*l).p1 as in the same as l->p1 ?
dukebound85
Nov 29, 2006, 10:31 PM
This didn't work. Says I have an incompatible type for argument 1 of a and incompatible type for arguement 2 of a.
how would I reference a pointer, line, which is a struct of points which are also a struct consisting of x and y values?
many thanks for the help, especially to iMeowbot and pilotError
MacCoaster
Nov 30, 2006, 12:09 PM
cool thanks, I'll try that when I get back to my computer
just wondering though, would that be equivalent to
(*l).p1 as in the same as l->p1 ?
Yes, both (*l). and -> dereference the pointer.
Your problem lies within your struct declaration for the line. The two variables within the struct are not pointers. Two ways of solving this. One way is to make the point variables pointers:
typedef struct point {
double x;
double y;
} point;
typedef struct line {
point * p1;
point * p2;
} line;
However, with this solution, when you make a line object you will need to use malloc to allocate memory for the points.
Or you can leave your struct be and change the arguments to be a plain point, instead of point *. But I believe it's more efficient to use pointers, as they would use less memory.
I hope I was clear.
savar
Nov 30, 2006, 02:26 PM
Yes, both (*l). and -> dereference the pointer.
Your problem lies within your struct declaration for the line. The two variables within the struct are not pointers. Two ways of solving this. One way is to make the point variables pointers:
Since he's apparently working from a book, I would guess that the structs are a given. Sounds to me like the distance function is expecting pointers to p1 and p2, not actually p1 and p2.
In that case: distance(&(l->p1), &(l->p2))
gnasher729
Nov 30, 2006, 06:00 PM
Hi all, I can not seem to get this to compile. Any ideas on what could be tripping it up? This is a .h file that I am trying to create on my own
What programming language are you using? Out of C, C++ and Objective C, only one would accept this header file.
dukebound85
Nov 30, 2006, 06:44 PM
thanks MacCoaster and Savar declaring them as pointers in the struct let it compile. I will need to work with malloc to store the pts for each line I assume.
Gnasher729, this is in C by the way
Once again thanks and I may need to ask for your assistance again in the near future!
MacCoaster
Nov 30, 2006, 07:41 PM
What programming language are you using? Out of C, C++ and Objective C, only one would accept this header file.
Eh? Care to explain? I've worked with all of them and all of their header files are .h files. C++ also accepts .hpp.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.