Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ScKaSx

macrumors regular
Original poster
Feb 27, 2006
105
0
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:

#define dVec Vector<double>

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

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

inline Point *new_Points(Contour_Points *cp)
{
Point *head= new Point;
Point *point= head;
point->xy=cp->xy;
while (NULL!=(cp=cp->next)) {
point=(point->next= new Point);
point->xy=cp->xy;
}
point->next=NULL;
return head;
}

I get the errors:
error:'struct Point' has no member named 'xy'
error:'struct Point' has no member named 'next'

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
 

ScKaSx

macrumors regular
Original poster
Feb 27, 2006
105
0
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
 

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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
 

ScKaSx

macrumors regular
Original poster
Feb 27, 2006
105
0
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:

error: redefinition of 'struct Point'

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
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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;
 

ScKaSx

macrumors regular
Original poster
Feb 27, 2006
105
0
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
 

ScKaSx

macrumors regular
Original poster
Feb 27, 2006
105
0
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
 

Attachments

  • contour.h.txt
    6.7 KB · Views: 145
  • PointsLines.h.txt
    2.1 KB · Views: 150
  • errors.txt
    4.3 KB · Views: 196

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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....
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.