Xcode Struct Help

Discussion in 'Mac Programming' started by ScKaSx, Dec 10, 2007.

  1. ScKaSx macrumors regular

    Joined:
    Feb 27, 2006
    #1
    Hi All,

    I am trying to use xcode to compile something that I got running through gcc in terminal. I hit a snag with some structs I am using. It seems simple enough, but I still keep getting errors. Anyways, I've defined the struct 'Point' in the following way:

    Now when I try to use this struct in the following code:

    I get the errors:
    I'm pretty sure I haven't setup my compiler correctly since I can compile this code from terminal using gcc. Does anyone have any suggestions why this wouldn't work in xcode and how I might be able to fix it?

    Cheers,
    ScKaSx
     
  2. ledd macrumors newbie

    Joined:
    Dec 26, 2006
    #2
    It works with gcc in terminal but not with the xCode build?
     
  3. ScKaSx thread starter macrumors regular

    Joined:
    Feb 27, 2006
    #3
    Yes, thats correct. The code I displayed is from header files I use for various programs. So far I've only used these with gcc from terminal, and they worked fine. Now, I'm trying to use these files with xcode (I'm using xcode because this program is alittle more complicated than the previous ones). To my surprise as well, I'm getting errors. This is what leads me to believe I've set up my compiler wrong or something to that effect.

    Cheers,
    ScKaSx
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    What compiler switches did you use to compile in the terminal? Compare that to what Xcode uses. I suspect it is that prehaps Xcode uses stricter compiler switches. Not sure, never really had a look at the default compiler options for Xcode.
     
  5. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #5
    It looks like Xcode is using the default C convention where typedef names are not automatically generated for struct tags (http://c-faq.com/struct/impltypedef.html)

    That is in your application code, include the struct keyword:

    struct Point *head= new Point;
    struct Point *point= head;

    Alternatively, keep the application code as is, but use a typedef in the header, for example:

    #define dVec Vector<double>

    typedef struct Point *PointPTR;

    struct Point{
    dVec xy;
    PointPTR *next;
    };

    The typedef is necessary for reasons explained here: http://c-faq.com/decl/selfrefstruct.html
     
  6. ScKaSx thread starter macrumors regular

    Joined:
    Feb 27, 2006
    #6
    Okay, so I tried to use struct tags, but it didn't work. I think the underlying problem is something else. After looking more closely at the errors I see that the first error is actually:

    which was first defined in the file MacTypes.h. Is this a standard header file that gcc 4.0.1 from terminal wouldn't use? Is there a work around that I can do to avoid these errors? Such as disable MacTypes.h or just some of its struct definitions? Thanks again

    Cheers,
    ScKaSx
     
  7. ScKaSx thread starter macrumors regular

    Joined:
    Feb 27, 2006
  8. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #8
    You probably need to change the type name you use. Types coming from the OS are usually a bad idea to override in practice.
     
  9. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #9
    It turns out the Point struct that's giving you trouble is defined as part of the old QuickDraw API (we're talking Mac OS 9 here). Somewhere, Xcode is configured to pull this in. You can view it here:

    /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Headers/MacTypes.h

    (incidentally, spotlight didn't find this..I had to do a 'locate MacTypes.h' in the terminal)

    This is an instance of the namespace management issue. You could either simply change the name of your Point type (e.g. to MyPoint), or else (assuming you're using C++) wrap your declaration of Point into a unique namespace, for example:

    //Point.h
    namespace foo
    {
    struct Point {...}
    }

    and either preface Point's usage in the .cpp file with the namespace or use the 'using namespace' directive

    //Point.cpp
    #include "Point.h"

    foo::point *head;
    using namespace foo;
    Point *point;
     
  10. ScKaSx thread starter macrumors regular

    Joined:
    Feb 27, 2006
    #10
    I'm still getting alot of errors. Thanks for your response yeroen, I think that will work, but I'm alittle confused on how to use the namespace.

    When and where do I put the preface 'foo::'?

    Can I put 'using namespace foo;' at the beginning of the cpp file?

    And can I use namespaces in header files, because I have header files that define structs using Point?

    Thanks a bunch!

    Cheers,
    ScKaSx
     
  11. ScKaSx thread starter macrumors regular

    Joined:
    Feb 27, 2006
    #11
    Btw, here are the two header files that are giving me problems.

    PointsLines.h defines the Struct Point and even uses it in the definition of other structs.

    contour.h uses Struct Point as well.

    I've also attached a list of the errors I've been getting. These files represent changes I made to represent the suggestions made by yeroen (thanks btw).

    Cheers,
    ScKaSx
     

    Attached Files:

  12. dcr macrumors member

    Joined:
    Jun 10, 2002
    #12
    Get rid of the "using" declarations and just refer to

    Points::point

    anywhere you need to reference your structure. The using keyword is dangerous (especially in header files) and reintroduces the ambiguity you're trying to resolve by putting your Point into its own namespace.

    http://www.gotw.ca/publications/migrating_to_namespaces.htm
     
  13. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #13
    I'd also dispense with the all inline declarations (even though the compiler is free to ignore them). The above poster included a link to Herb Sutter's guru of the week blog, if you read the wisdom dispensed therein and other places, you'll find a thorough discussion of inline declarations and how they entangle dependencies....
     

Share This Page