Reputation: 1203
I have been debugging my project for awhile now. All of my files are practically debugged however my Queue.h has issues connecting with them all.
I have a Queue.h file that seems to not process a large sum of my values.
I get issues with my current_queue pointer and other values like initial_rate refusing to be declared. I was hoping anyone could explain why they are not working.
Believe me, this list use to be twice as long.
UPDATE: I fixed the suggested issues with my () and so forth. I am surprised that those corrections corrected everything aside my qurrent_queue and initial_rate values. My errors are lower but those few values seem to hate me :(
ERRORS:
Queue.h:39: error: ISO C++ forbids declaration of ‘ListNode’ with no type
Queue.h:39: error: expected ‘;’ before ‘<’ token
Queue.h: In member function ‘void Queue<NODETYPE>::pop()’:
Queue.h:64: error: ‘current_queue’ was not declared in this scope
Queue.h: In member function ‘void Queue<NODETYPE>::push(const NODETYPE&)’:
Queue.h:70: error: ‘current_queue’ was not declared in this scope
Queue.h: In member function ‘void Queue<NODETYPE>::set_arrivalRate(double)’:
Queue.h:88: error: ‘initial_rate’ was not declared in this scope
Queue.h: In member function ‘int Queue<NODETYPE>::isEmpty() const’:
Queue.h:95: error: ‘current_queue’ was not declared in this scope
Queue.h: In member function ‘int Queue<NODETYPE>::getSize()’:
Queue.h:100: error: ‘current_queue’ was not declared in this scope
Queue.h: In member function ‘void Queue<NODETYPE>::check_new_arrival(int, bool)’:
Queue.h:113: error: ‘initial_rate’ was not declared in this scope
Queue.h:115: error: ‘current_queue’ was not declared in this scope
Queue.h:115: error: missing template arguments before ‘(’ token
Queue.h: In member function ‘int Queue<NODETYPE>::update(int, bool)’:
Queue.h:129: error: ‘current_queue’ was not declared in this scope
Here is my Queue.h file
#ifndef QUEUE_H
#define QUEUE_H
#include <string>
#include <queue>
#include <cstddef>
#include <algorithm>
#include <list>
#include <sstream>
#include "Passenger.h"
#include "Random.h"
#include <iostream>
extern Random simulation_obj;
using namespace std;
template <typename NODETYPE>
class Queue {
public:
Queue(string);
~Queue();
int isEmpty() const;
int getSize();
void check_new_arrival(int, bool);
void set_arrivalRate(double new_rate);
int get_totalWait() const;
int get_servedTotal() const;
void push(const NODETYPE& item);
void pop();
int update(int, bool);
string get_queue() const;
//NODETYPE& front();
//const NODETYPE& front() const;
private:
ListNode<NODETYPE> *current_queue;
int count_total;
int total_wait;
double initital_rate;
string QueueName;
};
template <typename NODETYPE>
Queue<NODETYPE> :: Queue(string name) : count_total(0), total_wait(0),
QueueName(name) {}
template <typename NODETYPE>
void Queue<NODETYPE> :: pop() {
current_queue -> removeFromHead();
}
template <typename NODETYPE>
void Queue<NODETYPE> :: push(const NODETYPE& item){
current_queue -> insertAtHead(item);
}
template <typename NODETYPE>
int Queue<NODETYPE> :: get_servedTotal() const{
return count_total;
}
template <typename NODETYPE>
int Queue<NODETYPE> :: get_totalWait() const {
return total_wait;
}
//set arrival
template <typename NODETYPE>
void Queue<NODETYPE> :: set_arrivalRate(double new_rate) {
initial_rate = new_rate;
}
template <typename NODETYPE>
int Queue<NODETYPE> :: isEmpty(){
// return true if the queue object is empty
return current_queue -> isEmpty();
}//end isEmpty method
template <typename NODETYPE>
int Queue<NODETYPE> :: getSize(){
return current_queue -> getSize();
}
template <typename NODETYPE>
string Queue<NODETYPE> :: get_queue() const {
return QueueName;
}
//See nitial_rate = new_rate;
template <typename NODETYPE>
void Queue<NODETYPE> :: check_new_arrival(int clock, bool display_all){
if (simulation_obj.next_double < initial_rate) {
current_queue.push(Passenger(clock));
if(display_all) {
cout <<"Time is " << clock << ": " << QueueName
<< " arrival, new queue size is "
<< current_queue.getSize() <<endl;
}
}
}
template <typename NODETYPE>
int Queue<NODETYPE> :: update(int clock, bool display_all) {
Passenger<NODETYPE> *next_customer = current_queue.pop();
//current_queue.pop();
int time = next_customer.get_initialTime();
int wait = clock - time;
total_wait += wait;
count_total++;
if(display_all) {
cout <<"Time is " << clock << ": Serving " << QueueName
<< " with time stamp at " << time << endl;
}
return clock + next_customer.get_processTime();
}
#endif
UPDATE:
I will include a few more of my files so that they may be help determining why these two values don't work
Passenger.h
#ifndef PASSENGER_H
#define PASSENGER_H
#include <iostream>
#include "Random.h"
//Use object from Checkout_Simulation.cpp
extern Random simulation_obj;
extern Random item_obj;
template <typename NODETYPE>
class Passenger {
public:
Passenger(int);
int get_initalTime();
int get_processTime();
// int get_id();
static void set_max_processTime(int max_processTime);
private:
//int id;
int processTime;
int initalTime;
static int max_processTimeInterval;
//sequence for passengers
//static int id_number;
};
//Get time Passenger arrived
template <typename NODETYPE>
int Passenger<NODETYPE> :: get_initalTime() {
return initalTime;
}
//Get process time of passenger
template <typename NODETYPE>
int Passenger<NODETYPE> :: get_processTime() {
return processTime;
}
template <typename NODETYPE>
void Passenger<NODETYPE> :: set_max_processTime(int max_processTime) {
max_processTimeInterval = max_processTime;
}
// Makes a new customer with their time that they arrived
template <typename NODETYPE>
Passenger<NODETYPE> :: Passenger(int inital){
//initalTime is from Passenger.h
initalTime = inital;
//processTime is from Passenger.h
processTime = 1 + simulation_obj.next_int(max_processTimeInterval);
}
//Max time to process passenger
template <typename NODETYPE>
int Passenger<NODETYPE> :: max_processTimeInterval;
#endif
Checkout_Simulation.h
#ifndef CHECKOUT_SIMULATION_H
#define CHECKOUT_SIMULATION_H
#include "Queue.h"
#include "Random.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
//Global random generator from Random.h
Random simulation_obj;
Random item_obj;
template <typename NODETYPE>
class Checkout_Simulation{
public:
int number;
static int const SIZE = 100;
int super_items[SIZE];
int ex1_items[SIZE];
int ex2_items[SIZE];
int regular_items[SIZE];
Checkout_Simulation() : super_express("Super Express Counter 01"),
express_line1("Express Line Counter 01"),
express_line2("Express Line Counter 02"),
regular_line("Regular Line Counter 01"),
clock_time(0), finish_time(0) {}
void run_sim();
void show_information();
void enter_information();
private:
void start_checkout();
Passenger<NODETYPE> *super_express;
Passenger<NODETYPE> *express_line1;
Passenger<NODETYPE> *express_line2;
Passenger<NODETYPE> *regular_line;
int super_express_max;
int processTime_max;
int total_time;
bool display_all;
int clock_time;
int finish_time;
// number of super_express served since last regular customer
int super_express_since_regular;
};
template <typename NODETYPE>
void Checkout_Simulation<NODETYPE> :: run_sim() {
for(clock_time = 0; clock_time <total_time; clock_time++){
item_obj.item_purchase(number);
if (number < 15) {
super_express.check_new_arrival(clock_time, display_all);
// number = the value
super_express.number = number;
}
else if (number >15 && number <=20){
express_line1.check_new_arrival(clock_time, display_all);
express_line2.check_new_arrival(clock_time, display_all);
}
else {
regular_line.check_new_arrival(clock_time, display_all);
}
if (clock_time >= finish_time){
start_checkout();
}
}//end for loop
}
template <typename NODETYPE>
void Checkout_Simulation<NODETYPE> :: start_checkout() {
/* if express lines are both empty and regular, and super_express_since_regular less
then super_expresses max hold, then update since_regular and super_express */
if( (!express_line1.empty() || !express_line2.empty())
&& ( (super_express_since_regular <= super_express_max) ||
regular_line.empty() )){
super_express_since_regular++;
finish_time = super_express.update(clock_time, display_all);
}//end if
//if the regular line isn't empty, super_express_since_regular = 0 and update regular
else if (!regular_line.empty()){
super_express_since_regular = 0;
finish_time = regular_line.update(clock_time, display_all);
}//end else if
// if regular_line is not empty and
//else if (!regular_line.empty() && super_express
else if (display_all){
cout << "Current time is " << clock_time <<": current counter is empty\n";
//}// end else if
}
}
template <typename NODETYPE>
void Checkout_Simulation<NODETYPE> :: show_information(){
cout << "\n The number of regular customers served was "
<< regular_line.get_servedTotal() << endl;
double average_wait = double(regular_line.get_totalWait())/
double(regular_line.get_servedTotal());
cout <<", with average waiting time of " << average_wait << endl;
cout <<"The number of express customers served in Express Line 01 was "
<< express_line1.get_servedTotal() <<endl;
average_wait = double(express_line1.get_totalWait())/
double(express_line1.get_servedTotal());
cout <<", with average waiting time of " << average_wait <<endl;
cout <<"The number of express customers served in Express Line 02 was "
<< express_line2.get_servedTotal() <<endl;
average_wait = double(express_line2.get_totalWait())/
double(express_line2.get_servedTotal());
cout <<", with average waiting time of " << average_wait <<endl;
cout <<"The number of super express customers served was "
<< super_express.get_servedTotal() <<endl;
average_wait = double(super_express.get_totalWait())/
double(super_express.get_servedTotal());
cout <<", with average waiting time of " << average_wait <<endl;
cout <<"Overall waiting time till all customers are helped is: "<<endl;
cout <<"Max length of super express line was: "<<endl;
cout <<"Average free time of super express line was:" <<endl;
}
template <typename NODETYPE>
void Checkout_Simulation<NODETYPE> :: enter_information() {
double input;
int max_processTime;
string response;
cout <<"Expected number of super_express customers per hour: ";
cin >> input;
super_express.set_arrivalRate(input/ 60.0);
cout <<"Expected number of express customers in Express Line 01 per hour: ";
cin >> input;
express_line1.set_arrivalRate(input/60.0);
cout <<"Expected number of express customers in Express Line 02 per hour: ";
cin >> input;
express_line2.set_arrivalRate(input/60.0);
cout <<"Expected number of regular customers per hour: ";
cin >> input;
regular_line.set_arrivalRate(input/60.0);
cout <<"Enter maximum number of super express customers served between express and regular customers ";
cin >> max_processTime;
cout <<"Enter total time in minutes for simulation: ";
cin >> total_time;
cout <<"Display simulation at a minute interval (Y or N) ";
cin >>response;
display_all = toupper (response[0]) == 'Y';
}
#endif
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include "ListNode.h"
using namespace std;
template <typename NODETYPE>
class LinkedList {
public:
LinkedList();
~LinkedList();
int getSize();
bool isEmpty();
void insertAtHead(NODETYPE & );
void insertAtTail(NODETYPE & );
//Change from bool to NODETYPE
NODETYPE removeFromHead();
NODETYPE removeFromTail();
void printList();
private:
int size;
ListNode<NODETYPE> *firstNode;
ListNode<NODETYPE> *lastNode;
ListNode<NODETYPE> *getNewNode(const NODETYPE &);
};
// Now you continue to implementation of LinkedList class using the template<typename NODETYPE> as appropriate in the program, you
//will implement all the public functions, constructor, destructor, and private function. Here is an example of the function getSize()
template<typename NODETYPE>
int LinkedList<NODETYPE>:: getSize() {
return size;
}
template <typename NODETYPE>
LinkedList<NODETYPE>:: LinkedList()
{
size = 0;
firstNode = NULL;
lastNode = NULL;
}
template <typename NODETYPE>
bool LinkedList<NODETYPE>:: isEmpty()
{
if(firstNode == NULL)
{
return true;
}
else
{
return false;
};
}
template<typename NODETYPE>
void LinkedList<NODETYPE>:: insertAtHead(NODETYPE & newData)
{
ListNode<NODETYPE> *newPtr = getNewNode(newData);
if(isEmpty())
{
firstNode = lastNode = newPtr;
// *lastNode = newData;
}
else
{
newPtr -> nextNode = firstNode;
firstNode = newPtr;
}
size++;
}
template<typename NODETYPE>
void LinkedList<NODETYPE>:: insertAtTail(NODETYPE & newData)
{
ListNode<NODETYPE> *newPtr = getNewNode(newData);
if(isEmpty())
{
firstNode = lastNode = newPtr;
}
else
{
lastNode -> nextNode = newPtr;
lastNode = newPtr;
}
size++;
}
template <typename NODETYPE>
NODETYPE LinkedList<NODETYPE>:: removeFromHead() {
// Edits for Stack
if (!isEmpty()) {
//makes a temp that is the firstNode
ListNode<NODETYPE> *tempPtr = firstNode;
if (firstNode == lastNode){ //or (size == 1)
firstNode = lastNode = NULL;
}
else {
firstNode = firstNode -> nextNode;
}
NODETYPE tempValue = tempPtr -> getData();
delete tempPtr;
size --;
return tempValue;
}
}
template<typename NODETYPE>
NODETYPE LinkedList<NODETYPE> :: removeFromTail()
{
if (!isEmpty()){
ListNode<NODETYPE> *tempPtr = firstNode;
// if only one Node
if (firstNode == lastNode ) {
firstNode = lastNode = NULL;
}
else{
//makes a temp that is the firstNode
//ListNode<NODETYPE> *tempPtr = firstNode;
while (tempPtr -> nextNode != lastNode){
tempPtr = tempPtr -> nextNode;
} //end while
//makes lastNode the one before the lastnode because temp isn't
//suppose to be lastnode due to while loop
lastNode = tempPtr;
// makes the temp go to next one which is lastnode
tempPtr = tempPtr -> nextNode;
// up to this point temp is lastNode so we cut link and make it null
lastNode -> nextNode = NULL;
}
NODETYPE tempValue = tempPtr -> getData();
delete tempPtr;
size --;
return tempValue;
}
}
/**
Method: printList
This method prints the list content on the screen
*/
template <typename NODETYPE>
void LinkedList<NODETYPE>:: printList()
{
if (isEmpty()) {
cout <<"The list is empty \n";
}
else {
// prefer while loop due to changing size
ListNode<NODETYPE> *currentPtr = firstNode;
cout <<"The list is: "<<endl;
while (currentPtr != NULL) {
cout << currentPtr -> data << "\n";
currentPtr = currentPtr -> nextNode;
}
cout << endl;
}
}
// Get new Node function
template <typename NODETYPE>
ListNode<NODETYPE> *LinkedList<NODETYPE> :: getNewNode(const NODETYPE & newData) {
return (new ListNode<NODETYPE>(newData));
}
//deconstructor
//delete everything in LinkedList if it is not empty
template <typename NODETYPE>
LinkedList<NODETYPE> :: ~LinkedList() {
if(!isEmpty()){
cout <<" Deleting all the nodes";
ListNode<NODETYPE> *currentPtr = firstNode;
ListNode<NODETYPE> *tempPtr;
while (currentPtr != NULL) {
tempPtr = currentPtr;
currentPtr = currentPtr -> nextNode;
delete tempPtr;
}//end while
cout <<" Release all memory \n";
}
}
#endif
ListNode.h
#ifndef LISTNODE_H
#define LISTNODE_H
#include <cstdlib>
using namespace std;
template<typename NODETYPE> class LinkedList;
template<typename NODETYPE>
class ListNode {
friend class LinkedList<NODETYPE>;
public:
ListNode(const NODETYPE &);
ListNode(const NODETYPE &, ListNode<NODETYPE> *);
NODETYPE getData();
void setData(NODETYPE &);
ListNode getNextNode() ;
void setNextNode(ListNode <NODETYPE>*);
private:
NODETYPE data;
ListNode<NODETYPE> *nextNode;
};
// Now the implementation of each function in ListNode class.
//We will be using template NODETYPE through out
template<typename NODETYPE>
ListNode <NODETYPE> :: ListNode(const NODETYPE &newData)
{
// This is a constructor for ListNode class in which we
//initialize the nextNode pointer to NULL, and assign data to
//newData. Please put in your code here
nextNode = NULL;
data = newData;
}
template <typename NODETYPE>
ListNode <NODETYPE>:: ListNode (const NODETYPE &newData, ListNode<NODETYPE> *newNextPtr) {
// This is a constructor for ListNode class in which we
//initialize the nextNode pointer to newNextPtr, and assign
//data to newData. Please put in your code here
data = newData;
// now the second variable ptr becomes the next node
nextNode = newNextPtr;
}
template <typename NODETYPE>
NODETYPE ListNode<NODETYPE>::getData() {
// This function returns the value of data
return data;
}
template <typename NODETYPE>
void ListNode <NODETYPE>:: setData( NODETYPE& newData) {
// This function sets data to the value of newData
data = newData;
}
template <typename NODETYPE>
ListNode<NODETYPE> ListNode<NODETYPE>:: getNextNode() {
// This function returns the nextNode pointer
return nextNode;
}
template <typename NODETYPE>
void ListNode<NODETYPE>:: setNextNode(ListNode<NODETYPE> *newNextPtr) {
// This function sets nextNode pointer to newNextPtr
nextNode = newNextPtr;
}
#endif
Random.h
#ifndef RANDOM_H
#define RANDOM_H
#include <cstdlib>
#include <ctime>
class Random {
public:
Random() {
srand((unsigned)time(0));
}
Random(int value) {
srand(value);
}
//Random number for processing time of customer
int next_int(int n) {
return int(next_double() * n);
}
//Return random integer range 0 -> 1
double next_double() {
return double(rand()) / RAND_MAX;
}
int item_purchase(int n) {
//make a case using a number 1-100(100 is max number of items they can have)
n = (rand() % 100) + 1;
return int(next_double() * n);
}
};
#endif
This is my last file which runs everything, however, the main line declaration of the sim_obj won't work. As you can see I have tried three attempts LOL
#include "Checkout_Simulation.h"
#include "Random.h"
#include <iostream>
#include <string>
#include <cctype>
#include "LinkedList.h"
using namespace std;
int main() {
//Checkout_Simulation sim_obj;
//Checkout_Simulation<int> sim_obj;
Checkout_Simulation<int> *sim_obj = new LinkedList<int>();
sim_obj.enter_information();
sim_obj.run_sim();
sim_obj.show_information();
return 0;
}
Upvotes: 0
Views: 420
Reputation: 2936
Reading all your posted code and taking a look at the errors it seems that something is wrong with your "ListNode" class.
Please check it again, if it is in the project and looks good, please, post it in your question and lets see what is going on there.
BTW this kind of error shows up when two headers include each other, please check this point also, and finally, check namespaces (naming collision or using a std name class without namespace for example)
Thanks!
Upvotes: 0
Reputation: 323
Why do you need constant method delcarations? I would get rid of const() completely on all of your methods. A couple of other things that are likely problematic: get_totalWait() has a void return type, you probably want either int or double for that. It's hard to know what else might be going wrong without more source code that includes the implementations of each method.
Upvotes: 1
Reputation: 57698
Erase all the ()
afer const
in your function declarations. That should be a good start.
Upvotes: 1