• Advertisement
Sign in to follow this  

Array of structs

This topic is 4342 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

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):
int howmanyininventory = ( a + b + c);

struct mouseoverstruct {		
	RECT weppos;
	int type;
			};

mouseoverstruct mouseovertest[howmanyininventory];

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).

Share this post


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


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Ok, I did like this:


mouseoverstruct * mouseovertest;
mouseovertest = new mouseoverstruct[howmanyininventory];



And it worked :)

Thanks guys!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Attala101
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.



mouseoverstruct * whatever = new mouseoverstruct[howmanyininventory];

Share this post


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

Share this post


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

Share this post


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

  • Advertisement