views:

86

answers:

2

Hello -

I have an iPhone app that used to use an array of several thousand small objects for its data source. Now, I am trying to make it use C++ Structs, to improve performance. I have written the struct, and put it in "Particle.h":

typedef struct{
   double changeX;
   double changeY;
   double x;
   double y;
}ParticleStruct;

Then, I imported "Particle.h", and attempted to define the array:

#import "Particle.h"
@implementation ParticleDisplay
struct ParticleStruct particles[];   
///Size will be determined later, this declaration is to make 
 ////the declaration visible to the entire class...

On this line, however, I get the error: "Array type has incomplete element type". Everything else compiles fine, as far as I can tell, and I am sure that "Particle.h" has been imported before the declaration. Any ideas?

+2  A: 

Since you have already typedefed it in Particle.h, drop the word struct from the array declaration line (the line where the error is).

HOWEVER,

  • In C++, you do not need to typedef it, just write struct Particle { /* members */ };

  • Why are you declaring an array without the length? Consider using std::vector (tutorial) which is a dynamically re-sizeable array (you don't have to bother about the length ever). It is simple as: std::vector< Particle > particles;

ArunSaha
I tried to use the vector, but #include <vector> throws an error, says no such file or directory.
Chris
@Chris: Hmm.. then it is something with the (iphone) development environment. I have no clue on that. Sorry.
ArunSaha
change your implementation file type to sourcecode.cpp.objcpp then your #include<vector> should work ( include in implementation file not header ).
Krzysztof Zabłocki
+1  A: 

Firstly, in c++ you don't need to typedef your structs. You don't need to use the struct keyword to declare a variable with some struct type:

 ParticleStruct *particles;

Use a pointer (as above) for your variable. Then you can dynamically allocate memory for it. Or better yet, use a vector:

vector<ParticleStruct> particles;

It seems that you need to familiarize yourself with c++. Consider reading a book or two.

JoshD
Ya, i've been considering learning the language outright, but I rarely use C++ in iPhone development, and when I need to I can usually scrape by with help from the internet... Maybe when I get a break from school...
Chris
@Chris: also, don't assume that c++ is more efficient that c. It can be, but if you don't know the language, you'll just expend a bunch of time and probably come out behind trying to re implement some c code.
JoshD