Help with Pointer Program *debugging*

Here are my Instructions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
TASK #1 – Define the Class
Write a class called ServerGroup, which will be a simple custom-made data structure.  It will be special-purpose, 
so it is NOT a template.  
It is used to represent serving people waiting in a line.

The private section will have a pointer to a 
dynamic integer array called servers, 
an integer variable called spServer, and an integer variable called freeServer.  
It will also be useful to have a variable representing the size of the array (number of elements).

The following member functions should be in the class:
•the constructor, which passes an integer into the function 
that is used to set the number of elements in the array (representing the number of general-purpose servers).  
It also sets the elements in the array to 0 and spServer to 0.
•A function called spServerFree, that will 
return true if SpServer is 0 and false otherwise (this represents the number of clock ticks until the special-purpose server is free; 
if the integer is 0, the server is already free).
•A function called serverFree, that returns true 
if it finds a 0 in the array and a false otherwise (these integers also represent the number of clock ticks 
until the corresponding servers are free).  
If it 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).
•A function called useServer that passes in an integer parameter, 
avTransTime, and set servers[freeServer] to avTransTime (avTransTime is the average transaction time, in clock ticks, to server a person in the line).
A function called usespServer, that will pass in an integer parameter,
 avTransTime, and set spServer to avTransTime (this is the same function as d) except that it is for the special-purpose servers where d) is for general purpose servers).
•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 is 0  4  5  0  0  9 (this is called when a click tick of time elapses).

Place the class files in files with appropriate names (i.e., ServerGroup.h and ServerGroup.cpp)

TASK #2 – Test the Class

You are to implement a program that uses the ServerGroup class.  
It should create a ServerGroup object, and then call all of the functions.  
The best way to do it is to write a temporary print function in the class to print out the values of the array and the other private values.  
When you know it works, just comment out this function (do not delete it!)

	
 

this is my header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ServerGroup
{
ServerGroup(int num_elements)
{

for (int i = 0; i <= num_elements; i++) { 
servers[i] = 0;
}

spServer = 0;
}

public:
void printvalues();
bool spServerFree();
bool serverFree();
void useServer(int avTransTime);
void usespServer(int avTransTime);
void decServers();

private:

int* servers;
servers = new int [10];
int spServer;
int freeServer;
};


this is my definition file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include "Fields_ServerGroup.h"


void ServerGroup::printvalues()
{
for (int i = 0; i <= num_elements; i++) 
{ 
cout << servers[i] << ' ' << endl;
}
cout << endl;
cout << spServer << endl;
cout << endl;
cout << freeServer << endl;
}


bool ServerGroup::spServerFree()
{
if(SpServer == 0)
return 1;
else
return 0;
}

bool ServerGroup::serverFree()
{
for (int i = 0; i <= num_elements; i++) 
{ 
if(servers[i] == 0)
{
freeServer = i;
return 1;
}
else
return 0;
}
}

void ServerGroup::useServer(int avTransTime)
{
Servers[freeServer] = avTransTime;
}

void ServerGroup::usespServer(int avTransTime)
{
SpServer = avTransTime;
}

void ServerGroup::decServers()
{
if(spServer != 0)
spServer--;
for (int i = 0; i <= num_elements; i++) 
{ 
if(servers[i] != 0)
servers[i]--;
}

}


and this is my implementation program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
//////////////////////////////////////////////////////////////////////////////////
////    Name of Program:  Program2                             	 		////
////    Submission Date:   Thurs. Oct. 10, 2013            	 	 	////
////    Names of Partners:   Byron Fields                   	 	       ////
////                         				                      ////
////     Description: Serving People in a Line				     ////
///////////////////////////////////////////////////////////////////////////////
*/


#include <iostream>
#include <iomanip>
#include <string>
#include "Fields_ServerGroup.h"

using namespace std;

int main()
{


int f;

cout << "Enter a number of elements" << endl;
cin >> f;

ServerGroup a(f);


a.spServerFree();
a.serverFree();
a.useServer(10);
a.usespServer(12);
a.decServers();
a.printvalues();
return 0;

}


and these are the errors i receive:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
In file included from Fields_program2.cpp:15:
Fields_ServerGroup.h:24: `num_elements' was not declared in this scope
Fields_ServerGroup.h:24: ISO C++ forbids declaration of `servers' with no type
Fields_ServerGroup.h:24: ISO C++ forbids initialization of member `servers'
Fields_ServerGroup.h:24: making `servers' static
Fields_ServerGroup.h:24: ISO C++ forbids in-class initialization of non-const 
   static member `servers'
Fields_ServerGroup.h:24: declaration of `int ServerGroup::servers'
Fields_ServerGroup.h:23: conflicts with previous declaration `
   int*ServerGroup::servers'
Fields_ServerGroup.h:24: duplicate member `ServerGroup::servers'
Fields_ServerGroup.h: In function `int main()':
Fields_ServerGroup.h:4: `ServerGroup::ServerGroup(int)' is private
Fields_program2.cpp:28: within this context
In file included from Fields_ServerGroup.cpp:2:
Fields_ServerGroup.h:24: `num_elements' was not declared in this scope
Fields_ServerGroup.h:24: ISO C++ forbids declaration of `servers' with no type
Fields_ServerGroup.h:24: ISO C++ forbids initialization of member `servers'
Fields_ServerGroup.h:24: making `servers' static
Fields_ServerGroup.h:24: ISO C++ forbids in-class initialization of non-const 
   static member `servers'
Fields_ServerGroup.h:24: declaration of `int ServerGroup::servers'
Fields_ServerGroup.h:23: conflicts with previous declaration `
   int*ServerGroup::servers'
Fields_ServerGroup.h:24: duplicate member `ServerGroup::servers'
Fields_ServerGroup.cpp: In member function `void ServerGroup::printvalues()':
Fields_ServerGroup.cpp:7: `num_elements' undeclared (first use this function)
Fields_ServerGroup.cpp:7: (Each undeclared identifier is reported only once for 
   each function it appears in.)
Fields_ServerGroup.cpp:9: `cout' undeclared (first use this function)
Fields_ServerGroup.cpp:9: `endl' undeclared (first use this function)
Fields_ServerGroup.cpp: In member function `bool ServerGroup::spServerFree()':
Fields_ServerGroup.cpp:20: `SpServer' undeclared (first use this function)
Fields_ServerGroup.cpp: In member function `void ServerGroup::useServer(int)':
Fields_ServerGroup.cpp:42: `Servers' undeclared (first use this function)
Last edited on
??
Line 24 in your header you cant do that. You must initialize in the constructor.
Line 7 in your definition you never declared num_elements. Line 22 the else is not necessary. line 28 num_elements not declared. The nice thing about compiler errors is it tell you excatlywhat line the error was on and what the error is...if there is a specific error you need help with ask. I don't feel like trying to figure out where the errors are since you didn't even include the lines. Just try and fix as many as you can then let us know if you need more help.
Thanks for the help man. For the most part ive gotten everything straightened. The only error im having issues with now is this one:

1
2
3
Fields_ServerGroup.h: In function `int main()':
Fields_ServerGroup.h:4: `ServerGroup::ServerGroup(int)' is private
Fields_program2.cpp:28: within this context


to me it seems that it is saying one of the variables in my constructor is private so i cant use it that way..

heres my header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class ServerGroup
{
ServerGroup(int num_elements)
{
servers = new int [num_elements];
for (int i = 0; i <= num_elements; i++) { 
servers[i] = 0;
}

spServer = 0;
}

public:
void printvalues(int num_elements);
bool spServerFree();
bool serverFree(int num_elements);
void useServer(int avTransTime);
void usespServer(int avTransTime);
void decServers(int num_elements);

private:

int* servers;
int spServer;
int freeServer;
};


my definition file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include "Fields_ServerGroup.h"
using namespace std;

void ServerGroup::printvalues(int num_elements)
{
for (int i = 0; i <= num_elements; i++) 
{ 
cout << servers[i] << ' ' << endl;
}
cout << endl;
cout << spServer << endl;
cout << endl;
cout << freeServer << endl;
}


bool ServerGroup::spServerFree()
{
if(spServer == 0)
return 1;
else
return 0;
}

bool ServerGroup::serverFree(int num_elements)
{
for (int i = 0; i <= num_elements; i++) 
{ 
if(servers[i] == 0)
{
freeServer = i;
return 1;
}
else
return 0;
}
}

void ServerGroup::useServer(int avTransTime)
{
servers[freeServer] = avTransTime;
}

void ServerGroup::usespServer(int avTransTime)
{
spServer = avTransTime;
}

void ServerGroup::decServers(int num_elements)
{
if(spServer != 0)
spServer--;
for (int i = 0; i <= num_elements; i++) 
{ 
if(servers[i] != 0)
servers[i]--;
}

}


my implementation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
//////////////////////////////////////////////////////////////////////////////////
////    Name of Program:  Program2                             	 		////
////    Submission Date:   Thurs. Oct. 10, 2013            	 	 	////
////    Names of Partners:   Byron Fields                   	 	       ////
////                         				                      ////
////     Description: Serving People in a Line				     ////
///////////////////////////////////////////////////////////////////////////////
*/


#include <iostream>
#include <iomanip>
#include <string>
#include "Fields_ServerGroup.h"

using namespace std;

int main()
{


int f;

cout << "Enter a number of elements" << endl;
cin >> f;

ServerGroup a(f);


a.spServerFree();
a.serverFree(f);
a.useServer(10);
a.usespServer(12);
a.decServers(f);
a.printvalues(f);
return 0;

}
Classes are by default set to private so you're going to want to move your constructor to public. So I would switch lines 3-11 in your header with line 13.
thanks for the help man. i finally got the program to compile and the euphoria has hit
Topic archived. No new replies allowed.