int howmanyininventory = ( a + b + c);
struct mouseoverstruct {
RECT weppos;
int type;
};
mouseoverstruct mouseovertest[howmanyininventory];
Array of structs
Hi
I'm having trouble making an array of structs. I do like this (a, b and c are integers whose values are calculated at runtime):
The compiler tells me:
c:\dev\wip3\wip2\playerinput.cpp(235) : error C2057: expected constant expression
c:\dev\wip3\wip2\playerinput.cpp(235) : error C2466: cannot allocate an array of constant size 0
c:\dev\wip3\wip2\playerinput.cpp(235) : error C2133: 'mouseovertest' : unknown size
Does this mean that I cannot use the "howmanyininventory"-integer to decide how many structs to make? Do I just have to pick a high number? (Making sure I will have enough mouseovertest-structs available, probably wasting memory).
The array if being statically created here so you must provide a constant inside the square brackets. For a dynamically sized collection simple new it on the heap like so:
Ofcourse a vector would be best here. A vector is an STL template designed to be a resizable array.
Hope that helps,
Dave
int howmanyininventory = ( a + b + c);struct mouseoverstruct { RECT weppos; int type; };mouseoverstruct* = new mouseovertest[howmanyininventory];
Ofcourse a vector would be best here. A vector is an STL template designed to be a resizable array.
Hope that helps,
Dave
it is becouse your
mouseoverstruct mouseovertest[howmanyininventory];
is a global variable and it is created at startup.
so you could as well just type in the size.
if you would say
function(int a, int b)
{
mouseoverstruct mouseovertest[a+b];
}
then this shouldnt give you an error
mouseoverstruct mouseovertest[howmanyininventory];
is a global variable and it is created at startup.
so you could as well just type in the size.
if you would say
function(int a, int b)
{
mouseoverstruct mouseovertest[a+b];
}
then this shouldnt give you an error
Quote:
mouseoverstruct* = new mouseovertest[howmanyininventory];
This gives me the error:
c:\dev\wip3\wip2\playerinput.cpp(235) : error C2059: syntax error : '='
Quote:
it is becouse your
mouseoverstruct mouseovertest[howmanyininventory];
is a global variable and it is created at startup.
It is not a global variable. The code I posted is all in the same function though. So they are of the same scope.
Ok, I did like this:
And it worked :)
Thanks guys!
mouseoverstruct * mouseovertest;mouseovertest = new mouseoverstruct[howmanyininventory];
And it worked :)
Thanks guys!
Quote:Original post by Attala101Quote:
mouseoverstruct* = new mouseovertest[howmanyininventory];
This gives me the error:
c:\dev\wip3\wip2\playerinput.cpp(235) : error C2059: syntax error : '='Quote:
it is becouse your
mouseoverstruct mouseovertest[howmanyininventory];
is a global variable and it is created at startup.
It is not a global variable. The code I posted is all in the same function though. So they are of the same scope.
mouseoverstruct * whatever = new mouseoverstruct[howmanyininventory];
Quote:Original post by Attala101
Ok, I did like this:
*** Source Snippet Removed ***
And it worked :)
Thanks guys!
Don't forget to "delete [] mouseovertest" when you're done with it...
i thought it was global because normaly you dont define a struct inside a function but in a header file or the global part of your cpp file.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement