C++ 未声明队列指针和值
我调试我的项目已经有一段时间了。我的所有文件实际上都已调试,但是我的Queue.h在连接它们时出现问题 我有一个Queue.h文件,它似乎没有处理我的大量值 我的当前\u队列指针和其他值(如初始\u速率拒绝声明)出现问题。我希望任何人都能解释他们为什么不工作 相信我,这张单子过去是原来的两倍长 更新:我修复了我的()等建议的问题。我很惊讶,除了我的当前队列和初始速率值之外,这些修正修正修正了一切。我的错误较低,但这几个值似乎讨厌我:( 错误:C++ 未声明队列指针和值,c++,compiler-errors,queue,undeclared-identifier,C++,Compiler Errors,Queue,Undeclared Identifier,我调试我的项目已经有一段时间了。我的所有文件实际上都已调试,但是我的Queue.h在连接它们时出现问题 我有一个Queue.h文件,它似乎没有处理我的大量值 我的当前\u队列指针和其他值(如初始\u速率拒绝声明)出现问题。我希望任何人都能解释他们为什么不工作 相信我,这张单子过去是原来的两倍长 更新:我修复了我的()等建议的问题。我很惊讶,除了我的当前队列和初始速率值之外,这些修正修正修正了一切。我的错误较低,但这几个值似乎讨厌我:( 错误: Queue.h:39: error: ISO C++
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
<代码>队列:h:39:错误:ISO C++禁止声明“没有任何类型的ListNoad”
Queue.h:39:错误:在“removeFromHead()之前应为“;”;
}
样板
无效队列::推送(常量节点类型和项){
当前队列->插入日期(项目);
}
样板
int Queue::get_servedTotal()常量{
返回总数;
}
样板
int Queue::get_totalWait()常量{
返回总等待时间;
}
//设定到达时间
样板
无效队列::设置到达率(新到达率的两倍){
初始利率=新利率;
}
样板
int队列::isEmpty(){
//如果队列对象为空,则返回true
返回当前_队列->isEmpty();
}//端空法
样板
int队列::getSize(){
返回当前_队列->getSize();
}
样板
字符串队列::get_Queue()常量{
返回队列名称;
}
//见初始利率=新利率;
样板
无效队列::检查新到达(整数时钟,布尔显示全部){
if(模拟对象下一个双倍<初始速率){
当前推送队列(乘客(时钟));
如果(全部显示){
无法在函数声明中删除所有的常量。这应该是一个好的开始。为什么需要常量方法增量?我会在所有方法上完全去掉常量。还有一些可能会有问题的事情:get_totalWait()如果返回类型为空,则可能需要int或double。如果没有包含每个方法实现的更多源代码,则很难知道还会出现什么问题。阅读所有发布的代码并查看错误,您的“ListNode”类似乎出了问题 请再次检查,如果它是在项目中,看起来不错,请张贴在您的问题,让我们看看那里发生了什么 顺便说一句,当两个头相互包含时会出现这种错误,请检查这一点,最后检查名称空间(例如命名冲突或使用没有名称空间的std name类)
谢谢!谢谢你指出这一点。我曾试图用这些行删除其他内容,但忘了删除它们。这纠正了我的大部分问题,但我的主要问题没有改变。你介意检查我上面的更新吗?谢谢托马斯!我自己解决了我的其他问题。谢谢我添加了Passenger.h文件,以便可能更容易理解它们是如何连接的。我添加了更新,留下了当前队列和初始速率值的主要问题。编译器似乎不知道NODETYPE是什么,因此您看到了一长串错误,因为您尝试在程序中多次引用它。您已经注释掉了这些行://NODETYPE&front();//const NODETYPE和front()const;我会再次尝试定义它。节点类型前面被注释掉了,因为它不需要。我试图尝试一些东西,但意识到我已经有了这样做的函数。我解决了我的问题。我将这个文件与一些函数分离成一个单独的passer_Queue.h文件,我的一个值有一个拼写错误。谢谢!我发布了一个我会更新我的文件,以便更好地解释我的问题。现在仍在尝试比较文件。标题的声明似乎不会相互重复。我一直被教导使用名称空间,因此不太熟悉在名称空间旁边使用std或不使用名称空间
#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
#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
#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
#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
#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
#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
#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;
}