Compile issues

Discussion in 'Mac Programming' started by dukebound85, Nov 28, 2006.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    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
     
  2. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #2
    double length(struct line *1);

    …and so on. Also the "*1" there looks like a typo.
     
  3. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #3
    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!
     
  4. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #4
    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.

    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?
     
  5. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #5
    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
     
  6. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #6
    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).
     
  7. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #7
    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
     
  8. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #8
    Try...

    double length(line *l)
    {
    return distance( l->p1, l->p2 );
    }
     
  9. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #9
    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 ?
     
  10. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #10
    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
     
  11. MacCoaster macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #11
    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.
     
  12. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #12
    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))
     
  13. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    What programming language are you using? Out of C, C++ and Objective C, only one would accept this header file.
     
  14. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #14
    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!
     
  15. MacCoaster macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #15
    Eh? Care to explain? I've worked with all of them and all of their header files are .h files. C++ also accepts .hpp.
     

Share This Page