Advertisement

Newbie Q: about functions in structures.....

Started by April 16, 2001 09:28 PM
3 comments, last by Narkster 23 years, 9 months ago
I got a kindof stupid question.... how do you define a variable inside a structure by using a function that goes through a set of complex calculations...
  
// test func

int dice(int sides, int rolls)
{
   // ....

   tempnumber = rand();
   return tempnumber;
}

struct stat_tag {
int health;
};

struct stat_tag list[4] = {
dice(5, 7);
}
  
this code is just fragmented but I think you get the jist of it. When I try to compile it I get the error: integer needs to be a constant. or something like that, is there anyway to do this? Thanx L8R -Nark
--------------------------Budha walks up to a hotdog vender and says:"Make me one with everything."L8R
When you do something like :

struct MyStruct array[10] = { ...Stuff... }

All that is in stuff is processed at compile time so you can only use consts.

To achieve what you want to do, you should initialize your structs at the beginnig of the program.

Note : if you use classes, you can use the constructor. Have a look at this :

  class stat_tag {public:    int health;    stat_tag() : health( dice(5,7)) { };};stat_tag list[4];   


[/source]
Advertisement
Thanks, but I don''t have my hands on a C++ compiler *yet*, so would this work in the same way as yours did?


  // same structure// defining statement:list[1].health = dice(6, 2);  


how about that? BTW, the borland command line stuff is C++, correct? Thanks again!!

L8R

-Nark
--------------------------Budha walks up to a hotdog vender and says:"Make me one with everything."L8R
Actaully, here would be a simpler way, I think

  struct stat_tag{int health;};int dice(int,int);void init(int[],int);int main(){   stat_tag list[4]; //creates an array instances of stat_tag called list   init(list,4); //initializes list//...do whatever to list from here   return 0;}void init(int list[],int size){   for(int x=0;x<size;x++) list[x]=dice(6,2);}int dice(int sides, int rolls){tempnumber = rand();   return tempnumber;}  


create an instance of stat_tag instead of making a new struct called list that inherits from stat_tag. Also, I don''t think you can say "struct stat_tag list[4];" because you are trying to make a new struct called list[4], and I don''t think that works.
Also, you could do this:
  struct stat_tag{   ...whatever} list[4];  

to make an array of type stat_tag

Are you even trying to be intelligent?
''cuz if you are, it ain''t workin''
Down with Tiberia!

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

First off "struct stat_tag list[4]" creates an array of structures using the stat_tag template. Second, the procedure that you are using set''s everything in the array of structures to a 6 sided die rolled two times, but the thing is that there are also char variables and different dice rolls that I need to perform to define other variables inside the structure. Sorry I didn''t make that clear.

L8R

-Nark
--------------------------Budha walks up to a hotdog vender and says:"Make me one with everything."L8R

This topic is closed to new replies.

Advertisement