Jump to content
  • Advertisement
Sign in to follow this  
poliet

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

This topic is 4314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[ i ] = 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.

Share this post


Link to post
Share on other sites
Advertisement
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 [ code ] or [ source ] tags, it really is useful!

PPS Mantear - that is valid code, it's an anonymous parameter. It's frowned upon because it doesn't give any indication of meaning.

Share this post


Link to post
Share on other sites
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 () ?

Share this post


Link to post
Share on other sites
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'

[ code ]

Ship::Ship(GLdouble initialPos_x, GLdouble initialPos_y, bool initialControl, bool initialSelected, GLdouble initialAlignment, String initialTexture, bool initialmouseover):Array TestArray (50)
{
shipnumber++;
pos_x = initialPos_x;
pos_y = initialPos_y;
control = initialControl;
selected = initialSelected;
alignment = initialAlignment;
shipTexture="warbird";
mouseover = initialmouseover;
SN=shipnumber;
AI=0;
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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'

[ code ]

Ship::Ship(GLdouble initialPos_x, GLdouble initialPos_y, bool initialControl, bool initialSelected, GLdouble initialAlignment, String initialTexture, bool initialmouseover):Array TestArray (50)


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:

AClass::AClass :
member (constructor_parameters)
{
}

Skizz

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!