Frage

I want to do this:

typedef struct
{
    CGPoint vertices[];
    NSUInteger vertexCount;
} Polygon;

But it says Field has incomplete type CGPoint [].

War es hilfreich?

Lösung

You need to do one of two things:

  1. Declare the array to be a fixed size (probably not what you want)
  2. Make it a pointer. But then you need to properly malloc and free the array as needed.

A better choice is to not use a struct and instead create a full class. Then you can add methods and properties as well as make memory management much easier. You are working in Objective-C. Take advantage of the Object Oriented aspects of the language. Add a method to calculate the circumference and area, etc. Put the logic where it belongs.

Andere Tipps

Set array size CGPoint vertices[count];

Don't you want a unique name for each element of your struct anyway? If you just want a bunch of CGPoint's in a numerical order, with the ability to count how many of them there are you'd be much better served by shoving them in an NSArray or NSMutableArray (stored as NSValue's of course)

The whole point of a struct would be to have easy access to the values by a descriptive name, ie:

typedef struct {
    CGPoint helpfulAndDescriptiveNameOne;
    CGPoint helpfulAndDescriptiveNameTwoWhichIsDifferentThanTheOtherName;
    etc...
    NSUInteger vertexCount;
}

For example, a CGRect is just a struct composed of four different CGFloats, each of which is descriptively and helpfully named:

typedef {
    CGFloat x;
    CGFloat y;
    CGFloat width;
    CGFloat height;
} CGRect;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top