Creating an array with a class constructor

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
fallingbrickwork
Posts: 52
Joined: Sun Jun 22, 2008 4:09 pm
Contact:

Creating an array with a class constructor

Post by fallingbrickwork »

Hi all,

I'm trying to setup an array with my class where i can input how large i want the array to be but can't seem to get it working.

Code: Select all

//Civs.h

class CCivs
{
public:
	CCivs( void );
	CCivs( int NoOfWaypoints ); //NoOfWaypoints is the size i want

	~CCivs(void);

private:
	int waypoints; // an int to hold the array?! - not sure what to do here?!
	
};

Code: Select all

// method in Civs.cpp

CCivs::CCivs( int NoOfWaypoints )
{
// I want to be able to create a new array with this size
	waypoints = new waypoints[NoOfWaypoints]; 
}
I realise what i'm doing is probably drastically wrong but i am very eager to learn the correct way of doing this.

Many thanks in advance and sorry to trouble you with something that'll probably be a basic programming problem.
:oops:

Many thanks, Matt.
Saumane
Posts: 27
Joined: Wed Jul 09, 2008 6:14 pm
Location: Finland

Post by Saumane »

int* Array;
Array = new int[100];
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

If you use Irrlicht's core types (or stl's) you can use the array (or vector) class to create a dynamic array.

Code: Select all

core::array<int> waypoints;

...

for( i=0; i< numWaypoints; ++i)
   waypoints.push_back( waypointValue );
If you use normal c++ types you need a pointer:

Code: Select all

int* waypoints;

...

waypoints = new int[ noOfWaypoints ];
This method has the disadvantage that you must handle all the memory management yourself. If you have more waypoints than fit in the array, you have to create a new one. When you no longer need the array you have to delete it. The array class does all that for you.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

When using the core::array you should try to call waypoints.reallocate(NoOfWayPoints) before pushing the values, as it'd otherwise reallocate in each loop step.
fallingbrickwork
Posts: 52
Joined: Sun Jun 22, 2008 4:09 pm
Contact:

Post by fallingbrickwork »

Firstly, many thanks for all the help people gave! :lol:

Can I just ask one semi-related question to take this further...

Why would you get an error C2059: Syntax error : 'constant'.

I have a gameState class and in the .h file within the private: I tried to create an instance of my CCivs class with: CCivs m_civ(10); so I could use it in all the methods of this gameState, ie. Draw, Update, Pause etc etc. Doing this threw up the constant error.

If on the other hand I made the same instance of the CCivs class in one of the gameState's actual methods (in the.cpp) it compiled fine (but then the scope would stop me being to be able to use it outside that method).

In the end I made the declaration as a global in this .cpp outside all the gameState methods.

Why does this happen??

Cheers...
Casus Belli - Realtime strategy war game
Image
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Without the code that generates the error we can only guess.

But from what you're saying I guess the code looks like this:

Code: Select all

...

private:
   CCivs m_civ(10);
};
Am I right?

If so: You can't initialize a member in the class definition. This has to be done in the constructor:

Code: Select all

Class::Class()
: m_civ(10)
{
}
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
fallingbrickwork
Posts: 52
Joined: Sun Jun 22, 2008 4:09 pm
Contact:

Post by fallingbrickwork »

That is correct...

Code: Select all

...
private:
CCivs m_civ(10);
Okay thank you... i'll look further at constructors.

Many Thanks.
Casus Belli - Realtime strategy war game
Image
Post Reply