未声明的队列指针和值

Queue Pointers and values not declared

我已经调试我的项目一段时间了。我的所有文件实际上都经过了调试,但是我的 Queue.h 与它们全部连接时出现问题。

我有一个 Queue.h 文件似乎无法处理我的大量值。

我的 current_queue 指针和其他值(如 initial_rate 拒绝声明时遇到问题。我希望有人能解释为什么他们不工作。

相信我,这个列表曾经是原来的两倍。

更新:我用 () 等修复了建议的问题。我很惊讶这些更正纠正了我的 qurrent_queue 和 initial_rate 值之外的所有内容。我的错误较低,但那几个值似乎讨厌我:(

错误:

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

这是我的 Queue.h 文件

#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

更新:

我将包含更多我的文件,以便它们可以帮助确定为什么这两个值不起作用

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

这是我最后一个运行一切的文件,但是,sim_obj 的主线声明将不起作用。如您所见,我尝试了三次 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;
}

删除函数声明中的所有 () afer const。这应该是一个好的开始。

为什么需要常量方法声明?我会在你的所有方法上完全摆脱 const() 。其他一些可能有问题的事情: get_totalWait() 有一个 void return 类型,你可能想要 int 或 double 。如果没有包含每个方法的实现的更多源代码,很难知道还会出现什么问题。

阅读您所有的 posted 代码并查看错误,您的 "ListNode" class.

似乎有问题

请再次检查,如果它在项目中并且看起来不错,请post它在你的问题中,让我们看看那里发生了什么。

顺便说一句,当两个 headers 相互包含时会出现这种错误,请同时检查这一点,最后检查命名空间(命名冲突或使用没有命名空间的标准名称 class示例)

谢谢!