I want to create a class one of whose private: members is a struct point(see below). The public members ndim and numparticles are
set at run-time by the user, which are used to create corresponding arrays inside the class . However I am getting a compiler error. I don’t understand where I messed up.
The compiler error being displayed:
nbdsearch.h:25: error: ‘point’ does not name a type
nbdsearch.h:24: error: invalid use of non-static data member ‘nbdsearch::ndim’
nbdsearch.h:31: error: from this location
nbdsearch.h:31: error: array bound is not an integer constant before ‘]’ token
The class code:
class nbdsearch{
public:
int ndim,numparticles;
point particlevec[numparticles];
private:
struct point{
string key;
double cood[ndim];
};
};
Array size needs to be a compile time constant and
ndimclearly isn’t.ndimis a variable.On line 25, compiler doesn’t know what
pointis. The structure is defined at a later point. Compilers in C++ work on a top to bottom approach(Not sure whether C++0X relaxes this rule). So, what ever types being used should be known to it before hand.Try this –