Please Help with C++ Class Object! (syntax error)

Started by
19 comments, last by Saruman 17 years, 6 months ago
Hello All! I try to initialize an Object of an Array class with a certain size in my other main Class, but it wont work that easy. My mainclass shortend looks like this.. class Ship { public: Ship(GLdouble initialPos_x=0, GLdouble intitialPos_y=0, bool initialControl=0, bool initialSelected=0, GLdouble initialAlignment=0, String initialTexture="shipmap.tga, 1", bool initialMouseOver=FALSE); Ship (const Ship &); ~Ship(); . .. ... private: . .. ... Array DevianceAngle; Array AngleToEnemies(32); . .. ... } And my Array Class looks like that: class Array { public: Array(int = 5); Array(const char *const); Array(const Array &); ~Array(); int getSize() const; float Array::GetLast(void); void Clean(int f, int l, float val); Array Clean(float val); Array Add(float num); Array Insert(int pos, float num); void SetSize (int s) { size = s;} float GetMax(void); float GetMin(void); void setVal(int num, float val); const Array &operator=( const Array & ); float &operator[]( int ); float operator[]( int ) const; bool operator==( const Array & ) const; bool operator!=( const Array &right ) const { return ! ( *this == right ); } private: int size; // pointer-based array size float *ptr; // pointer to first element of pointer-based array }; Array::Array( int arraySize ) { size = ( arraySize > 0 ? arraySize : 10 ); // validate arraySize ptr = new float[ size ]; // create space for pointer-based array for ( int i = 0; i < size; i++ ) ptr = 0; } . .. … Array DevianceAngle initializes just fine with the standard size = 5, but if I try to initialize AngeToEnemies it writes this message: E:\C++ Projects\Game\game.cpp(185) : error C2059: syntax error : 'constant' Why?? How can I initialize an Array Object in my Ship Class with a different size and without getting the syntax error? Please help.
Advertisement
"Array(const char *const);"

You can't have a variable named 'const' since it's a reserved keyword.
You can only specifiy constructor arguments when an object is being constructed. The line:

Array AngleToEnemies(32);

should be a declaration. You need to construct the AngleToEnemies object in the Ship constructor thus:

Ship::Ship (/*parameters*/) :
AngleToEnemies (32)
{
}

Skizz

PS use or [ source ] tags, it really is useful!<br><br>PPS Mantear - that is valid code, it's an anonymous parameter. It's frowned upon because it doesn't give any indication of meaning.
thanks for the reply but I still not exactly sure what you mean..

the error refers to: Array AngleToEnemies(32)

if I am outside my class I can easily initialize any Array Object of any size e.g. Array anArray(40);.. creates an Array of size 40.
But if I initialize in in Class, it stipulates the mentioned syntax error.

I still don't understand how to solve this problem... what should I change, to be able to initialize an Array object from my Array class in my Ship class with a size that is defined by brackets () ?

I tried this now as suggested earlier

and still getting an errormessage..
E:\C++ Projects\Game\game.cpp(226) : error C2061: syntax error : identifier 'TestArray'

<br><br>Ship::Ship(GLdouble initialPos_x, GLdouble initialPos_y, bool initialControl, bool initialSelected, GLdouble initialAlignment, String initialTexture, bool initialmouseover):Array TestArray (50)<br>{<br> shipnumber++;<br> pos_x = initialPos_x;<br> pos_y = initialPos_y;<br> control = initialControl;<br> selected = initialSelected;<br> alignment = initialAlignment;<br> shipTexture="warbird";<br> mouseover = initialmouseover;<br> SN=shipnumber; <br> AI=0;<br> }<br>
Hmmm, this is tricky to explain. It's all to do with the difference between declaration and instantiation. You can't provide any constructor arguments when declaring, only when you're instantiating. At global scope:

Array AngleToEnemies(32);

is an instantiation, you can provide a constructor parameter.

However:

class AClass{private:  Array AngleToEnemies;};

Is a declaration and you can't provide a constructor parameter. The reason the above works is that the default constructor is called (=5) when an instance of AClass is instantiated. Therefore, the only place you can provide a constructor parameter to a member variable is in the constructor of the owning class:

class AClass{public:  AClass (void) :    AngleToEnemies (32) // this is the only place the parameter can be specified  {  }private:  Array AngleToEnemies;};

Try it and see. It's a complex idea to understand so you can either accept it as given or try searching for more in-depth descriptions of object construction.

Skizz
Quote:Original post by poliet
I tried this now as suggested earlier

and still getting an errormessage..
E:\C++ Projects\Game\game.cpp(226) : error C2061: syntax error : identifier 'TestArray'

<br><br>Ship::Ship(GLdouble initialPos_x, GLdouble initialPos_y, bool initialControl, bool initialSelected, GLdouble initialAlignment, String initialTexture, bool initialmouseover):Array TestArray (50)<br><!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>You still need to declare the member as you would any other member but without the constructor parameters, you need to initialise the object in the owning class' constructor using the form:<br><pre><br>AClass::AClass :<br> member (constructor_parameters)<br>{<br>}<br></pre><br>Skizz<br><br>
Quote:Original post by Mantear
"Array(const char *const);"


That isn't declaring a variable named "const", it's specifying that the ctor takes a const pointer (meaning no ++ or -- =) to a const char.

daerid@gmail.com
thank for all your help.. I got it now...
Since this is clearly C++ why are you using your own Array class instead of the standard, highly efficient dynamic-array class std::vector?

Σnigma

This topic is closed to new replies.

Advertisement