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.
#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)
returntrue; returnfalse;
}
private:
int *servers = newint[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.
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 = newint[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)
returntrue; returnfalse;
}
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)
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 = newint[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)
returntrue; returnfalse;
}
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)
returntrue; returnfalse;
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...
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
returntrue;
}
}
returnfalse; //Went through entire array and found no free server.
}
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.
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
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;
};
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).
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
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.