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

Pages: 12
Sep 16, 2013 at 12:26am
Hey guys. I've got kind of a silly question here so bear with me. I've got a bool function in my .h but when I try and put it in my .cpp my program doesn't compile. It just shoots a ton of error messages at me so I could really use some help.

my .h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef ServerGroup_h
#define ServerGroup_h

class ServerGroup
{
public:
  ServerGroup(int size); // pass in an integer parameter used to set the number of elements in the dynamic integer array
  
  bool spServerFree(ServerGroup a)
  {
    if (spServer == 0)
      return true; return false;
  }
  
private:
  int *servers = new int[size]; // needs pointer to a dynamic integer array called servers
  int spServer; // special purpose server
  int freeServer;
  int size; // for number of elements in the dynamic array
};

#endif  


.cpp file
1
2
3
4
5
6
7
#include "ServerGroup.h"

ServerGroup::ServerGroup(int num) // pass in an integer parameter used to set the number of elements in the dynamic integer array
{
  size = num;  // the constructor will also set all elements of the array to 0
  spServer = 0; // set spServer to 0
}


Is my bool function written incorrectly? Because why isn't my .cpp liking my bool function when I put it in there?


The bool function is basically there to to return true is spServer is 0 and else return false.
Last edited on Sep 16, 2013 at 12:27am
Sep 16, 2013 at 12:51am
closed account (N36fSL3A)
I don't understand why spServerFree takes a ServerGroup object.
Sep 16, 2013 at 12:58am
> Because why isn't my .cpp liking my bool function when I put it in there?
It may be a good idea to show the code that is causing the error

By the way, ¿why you don't use the parameter of the function?


Also int *servers = new int[size]; ¿what value does `size' hold when that line is invoked?
Sep 16, 2013 at 1:02am
The prompt is "Write a function called spServerFree that will return true if spServer is 0 and return false otherwise."

What about something like this?

.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef ServerGroup_h
#define ServerGroup_h

class ServerGroup
{
public:
  ServerGroup(int size); // pass in an integer parameter used to set the number of elements in the dynamic integer array
  
  bool spServerFree(int a);
  
private:
  int *servers = new int[size]; // needs pointer to a dynamic integer array called servers
  int spServer; // special purpose server
  int freeServer;
  int size; // for number of elements in the dynamic array
};

#endif  


.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "ServerGroup.h"

ServerGroup::ServerGroup(int num) // pass in an integer parameter used to set the number of elements in the dynamic integer array
{
  size = num;  // the constructor will also set all elements of the array to 0
  spServer = 0; // set spServer to 0
}


ServerGroup::bool spServerFree(int a)
{
  a = spServer;
  if (spServer == 0)
    return true; return false;
}



It doesn't compile but is my logic better now? I'm getting an error saying expected unqualified ID at ServerGroup::bool spServerFree(int a)
Sep 16, 2013 at 1:05am
bool is not a member of ServerGroup. Drop the scope. Move the scope to the function name.

Your allocation on line 12 is still incorrect. What would size be?
Last edited on Sep 16, 2013 at 1:10am
Sep 16, 2013 at 1:23am
bool ServerGroup::spServerFree(int a)
you still don't use the parameter...
Sep 16, 2013 at 1:47am
Thanks for the tips I really appreciate it. I'm actually not sure what to make size. In our instruction is says "The private section of ServerGroup class will have a pointer to a dynamic integer array called servers, an integer variable called spServer and an integer variable called freeServer. Also have a variable called size for the number of elements in the dynamic array.

Since they didn't tell me what to make size should I just assign my own value for size?

I've got this now

.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef ServerGroup_h
#define ServerGroup_h

class ServerGroup
{
public:
  ServerGroup(int size); // pass in an integer parameter used to set the number of elements in the dynamic integer array
  
  bool spServerFree(int a);
  
private:
  int *servers = new int[size]; // needs pointer to a dynamic integer array called servers
  int spServer; // special purpose server
  int freeServer;
  int size=0; // for number of elements in the dynamic array
};

#endif  


.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "ServerGroup.h"

ServerGroup::ServerGroup(int num) // pass in an integer parameter used to set the number of elements in the dynamic integer array
{
  size = num;  // the constructor will also set all elements of the array to 0
  spServer = 0; // set spServer to 0
}


bool ServerGroup::spServerFree(int a)
{
  a = spServer;
  if (spServer == 0)
    return true; return false;
}
Last edited on Sep 16, 2013 at 1:53am
Sep 16, 2013 at 2:03am
Or... You could do all initialization in the constructor:
1
2
3
4
5
6
ServerGroup::ServerGroup(int num)
   : servers(new int[num])
   , spServer(0)
   , freeServer(0)
   , size(num)
{}


You should also consider assigning 0 to all of the elements of servers to make sure there is no garbage data.

Lastly:
ne555 wrote:
you still don't use the parameter[in ServerGroup::spServerFree()]...
Sep 16, 2013 at 3:15am
Thanks Daleth for the tips. But don't I use spServerFree? In my .h I have got bool spServerFree(int a); and in my .cpp I've got
1
2
3
4
5
6
bool ServerGroup::spServerFree(int a)
{
  spServer = a;
  if (spServer == 0)
    return true; return false;
}


Where I have the int a passed into spServer and then I compare if it's equal to 0 or not. Is the way I have my code written incorrect?

Sep 16, 2013 at 3:17am
In your last post, line 12 was reversed. Your current one makes more sense.
Sep 16, 2013 at 3:41am
Thanks! I'm moving on to the next part of my program where it's asking us to write a function called serverFree that will return true if it finds a 0 in the array and false otherwise. If the function finds a 0 in the array it will set freeServer to the index of that element. (freeServer has the index of one of the servers that are free)

I've got this for my function
1
2
3
4
5
6
7
8
9
10
bool ServerGroup::serverFree(int b)
{
  size = b;
  for (int i = 0; i < size; i++)
  {
    if (size == 0)
      return true; return false;
      freeServer = &size;
  }
}


But i've got an error on freeServer = &size;

I have for my for loop to look through my array and see if there are any 0's and then assign the index of that element to freeServer...
Last edited on Sep 16, 2013 at 3:59am
Sep 16, 2013 at 4:03am
You aren't even looking through your array... And you changed your size variable (which I think should be const unless you are allowed to change the number of servers).

What is variable b for? Is it the start of where to look?
You iterate from 0 to b, but you only check if size is 0, which it will only be true if b is 0. The for loop is pointless in this regard.
Next, on line 7, you guarantee that the function will exit no matter what. Line 8 will never be reached. For that matter, the loop only runs once.
Line 8 is problematic because you attempt to assign a memory address to an int. int = int*
And there is no reason for you to assign freeServer with size anyway. According to your specification, you want to assign freeServer with the index, not size.

Anyway, here is a basic function with some explanations:
1
2
3
4
5
6
7
8
9
10
bool ServerGroup::serverFree(){
   for(int I(0); I < size; ++I){
      if(servers[I] == 0){ //Note how I am accessing servers at an index
         freeServer = I; //Now assign freeServer with the index
         return true;
      }
   }

   return false; //Went through entire array and found no free server.
}
Last edited on Sep 16, 2013 at 4:05am
Sep 16, 2013 at 4:18am
Daleth: thank you very much for your in depth explanation. Honestly I'm not sure why I even passed in int b. For some reason I thought I needed to pass in the value I needed to retrieve the index for so my thought process was to assign the int b to the variable I'd get the index.

This brings me to my final question.

The last part of my lab is asking the following.
Write a function called decServers that will decrement spServer by 1, unless spServer is already 0 (if it is 0 it stays the same). The decServers function will also decrement each element in the array by 1, unless the element is already 0. For example if the array is 0 5 6 0 0 10 then after decServers is called the array will be 0 4 5 0 0 9.

Would I implement a similar sorting through array function like the one Daleth provided above? I'm trying to figure this out before I do any sort of writing yet. I don't want to implement a brute force tactic of solving this until I understand exactly how I'm going to write this.
Sep 16, 2013 at 4:29am
Psuedo-Code:

Reallocate memory for size-1 space
0) If the size is already 0, exit the function
1) Have a pointer temporarily point to old space
2) Allocate new space and point to it with servers
   2.a) Should probably assign all elements to 0 to avoid garbage
3) Copy over old data
4) Free old space
5) Decrement size

Go through servers array and decrement each element unless it is already 0
Sep 16, 2013 at 4:51am
Daleth thanks for the psuedo-code. I'm trying to work through steps 0 - 3 of it but I'm pretty sure it isn't correct. I'm really sorry to keep bothering you with this but if you could point me in the right direction I'd greatly appreciate it.

The for loop I'm using is trying to look through the array and find if there are any 0's. If it see's a 0 it will break. Next I have created a temporary pointer and I'm trying to point to old space. I'm not too sure what "old space is" so I've got it pointing to servers which I don't believe is what you wanted me to do. Step 2 you say to allocate new space and point to it with servers. This... I'm not too sure how to go about implementing it because I've just assigned tempPtr to servers...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int ServerGroup::decServers()
{
  for(int I(0); I < size; ++I) 
  {
    if(servers[I] == 0)
    {
      break;
    }
    
    int *tempPtr;
    tempPtr = servers;
    
    spServer -= 1;
};
Sep 16, 2013 at 4:59am
Oh, I'm really sorry. I misread your previous post (I tend to do that a lot...). Turns out you don't need to do any reallocation at all (sorry again). So really, besides decrementing spServer, all you'd have to do is decrement the elements in servers.

Just decrement spServer if it's not 0. For example:
1
2
3
int num(4);

if(num != 0) --num; //This -- operator is shorthand for num -= 1. 


Lines 3 to 8 looks fine besides missing an ending brace for your for loop, but since you're already iterating through the array, you may as well take care of any decrementing, right?
5
6
if(servers[I] != 0)
   --servers[I];


Also, what are you going to return?

Lastly, you don't need semi-colons after function bodies (line 14).
Sep 16, 2013 at 5:19am
Oh no worries Daleth. Ok so how does this work? Also, I want to return servers but I don't think I've got it right.

Here's what I've got

1
2
3
4
5
6
7
8
9
int ServerGroup::decServers()
{
  for(int I(0); I < size; ++I) 
  {
    if(servers[I] != 0)
      --servers[I];
}
  return servers;
}


Also, if I wanted to go about testing this function with values would you know how I would go about doing so?
Last edited on Sep 16, 2013 at 5:20am
Sep 16, 2013 at 5:27am
Hm... Why do you want to return a pointer to your raw, internal array?
Anyway, what you return must match up with the return type. servers is int*, so your return type should be the same.

Don't forget to decrement spServer if it is not 0.

For testing your function, you could take the definition out and test it separately, like
 
int* decServers_test(int[] tmpServers, int& tmpSPServer);

I only suggest this because your current class provides no way to modify the elements of servers from the outside.
Sep 16, 2013 at 5:33am
Hm that kind of makes sense. So would I return *servers?

Here's what I've got

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];
    else break;
}
  spServer -= 1;
  
  return *servers;
}
Sep 16, 2013 at 5:38am
That returns the first value in servers. Take a closer look at my example in my last post.

Don't forget to check if spServer is already 0.

I don't think line 7 is necessary. You're array could look like {0, 5, 0, 9}, and you're expecting it to be changed to {0, 4, 0, 8}. break will exit the entire loop body. You could use continue, but it's also not really needed.
Pages: 12