Help moving my bool function from my .h to .cpp file

Pages: 12
Sep 16, 2013 at 5:43am
Ah I think I've got it. I want to return servers[I] because that is what we are decrementing previously correct?

How is this?
1
2
3
4
5
6
7
8
9
10
11
12
int ServerGroup::decServers()
{
  for(int I(0); I < size; ++I) 
  {
    if(servers[I] != 0)
      --servers[I];
}
  if(spServer!= 0)
  spServer -= 1;
  
  return servers[I];
}
Sep 16, 2013 at 5:49am
The variable I only exists within the for loop body. If you want to return the last item decremented, declare I outside of the for loop.
Sep 16, 2013 at 5:55am
Declare I outside of the for loop? Do you mean something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
int ServerGroup::decServers()
{
  for(int I(0); I < size; ++I)
  {
    if(servers[I] != 0)
      --servers[I];
  }
  if(spServer!= 0)
    spServer -= 1;
 
  int I(0);
  return servers[I];
}
Sep 16, 2013 at 6:03am
No... Because that is as good as return servers[0];. You stated the returned value will be the last decremented item, correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int ServerGroup::decServers()
{
  int I(0); //<--Here
  for(; I < size; ++I)
  {
    if(servers[I] != 0)
      --servers[I];
  }
//By this point, I will be the index of the last decremented element
  if(spServer!= 0)
    spServer -= 1;
 
  return servers[I];
}
Last edited on Sep 16, 2013 at 6:03am
Topic archived. No new replies allowed.
Pages: 12