I want to dynamically allocate the required amount of instances of the class character{...};
after the programs entry point so that I can represent the total amount of players (characters) in the game,the total amount being loaded in from a text file thus the reason for dynamic allocation..
But how do I tell my header files that.....
1]The new instances will exist even though they have not been declared at compile time?
I can't use the name of the dynamically allocated instance in any of the header or .cpp files cause they (the instances) dont exist yet.
Do I need to use a pointer to the class?
Or work out how to pass in command line arguments?
(secondary thoughts)
Ive been told that the CodeBlocks IDE nightly builds are very good.
Im torn between wxDev-cpp and trying CodeBlocks,do you think that wxDev-cpp is
better?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Answering your second question. Try them both! You can have them installed
side by side.
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-09-29
class cExample
{
...
int member ;
...
}
// Declare a pointer to the array of dynamic instances (pointer-to-pointer)
class cExample** objets ;
// Determine how many instances to create (however you like - just a contrived example)
int count = getNumberOfInstances() ;
// Instantiate an array of 'count' instances.
objects = new cExample[count] ;
// Access an element in the array.
objects[n]->member = x ;
wxDev-C++ is Dev-C++ with GUI designer and GUI class library. Code::Blocks like Dev-C++ is simply an IDE with few frills. That is to say they address different problems. If Visual GUI development is what you want Code::Blocks will not help.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-09-29
... w.r.t. command line arguments, you could just Google it, but if your program were invoked thus:
myprog 10
with the intent of creating ten instances:
int main( int argc, char** argv )
{
int count = 0 ;
.....In function main(int, char**)
base operand of '->' has non-pointer type 'cExample'
base operand of '->' has non-pointer type 'cExample'
[build error][test.o]Error 1
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-09-30
No at all, today it seems that I don't know what I am doing. Each element in teh array is an actual insrtance of cExample, not a pointer, so it should be:
objects[0].data = 17 ;
std::cout << objects[0].data ;
Which is exactly what the compiler was telling you.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello everyone,
I hope your all well.
I want to dynamically allocate the required amount of instances of the class character{...};
after the programs entry point so that I can represent the total amount of players (characters) in the game,the total amount being loaded in from a text file thus the reason for dynamic allocation..
But how do I tell my header files that.....
1]The new instances will exist even though they have not been declared at compile time?
I can't use the name of the dynamically allocated instance in any of the header or .cpp files cause they (the instances) dont exist yet.
Do I need to use a pointer to the class?
Or work out how to pass in command line arguments?
(secondary thoughts)
Ive been told that the CodeBlocks IDE nightly builds are very good.
Im torn between wxDev-cpp and trying CodeBlocks,do you think that wxDev-cpp is
better?
Answering your second question. Try them both! You can have them installed
side by side.
Wayne
class cExample
{
...
int member ;
...
}
// Declare a pointer to the array of dynamic instances (pointer-to-pointer)
class cExample** objets ;
// Determine how many instances to create (however you like - just a contrived example)
int count = getNumberOfInstances() ;
// Instantiate an array of 'count' instances.
objects = new cExample[count] ;
// Access an element in the array.
objects[n]->member = x ;
wxDev-C++ is Dev-C++ with GUI designer and GUI class library. Code::Blocks like Dev-C++ is simply an IDE with few frills. That is to say they address different problems. If Visual GUI development is what you want Code::Blocks will not help.
Clifford
... w.r.t. command line arguments, you could just Google it, but if your program were invoked thus:
myprog 10
with the intent of creating ten instances:
int main( int argc, char** argv )
{
int count = 0 ;
if( argc > 1 )
{
count = atoi( argv[1] )
}
if( count > 0 )
{
objects = new cExample[count] ;
...
}
...
}
Clifford
Thanks alot for the examples Clifford.
Now I can do it,yipee.
Spoke a little too early.
In your example Clifford that I used the compiler tells me that...
class cExample** object ;
object = new cExample[ count ] ;
.....cannot convert patrons to patrons* in assignment.
any chance of some feed back?
sorry,it says...
cannot convert cExample to cExample* in assignment.
Sorry:
// Declare a pointer to the array of dynamic instances (pointer-to-pointer)
class cExample* objects ;
They do say "there is no problem that cannot be solved with one more level of indirection", here you needed one less! ;-)
Clifford
okay,
I know your the one that knows what he's doing,
but I get a new error with one level of indirection.
heres the test code...
//all on main source file
class cExample{
public:
cExample(){} ;
~cExample(){} ;
int main()
{
class cExample* objects ;
int count = 5 ;
}
.....In function main(int, char**)
base operand of '->' has non-pointer type 'cExample'
base operand of '->' has non-pointer type 'cExample'
[build error][test.o]Error 1
No at all, today it seems that I don't know what I am doing. Each element in teh array is an actual insrtance of cExample, not a pointer, so it should be:
objects[0].data = 17 ;
std::cout << objects[0].data ;
Which is exactly what the compiler was telling you.
Clifford