当我弹出我的模板堆栈中的项目并打印弹出的值时,始终为数字1。我已经推入堆栈。这里是我的课程和主文件。希望你们中的一位专家能够弄清楚我搞砸了什么。从堆栈返回的C++值为1,无论我推入堆栈
堆栈模板类
#pragma once
#include <iostream>
#include <string>
#include <stack>
using namespace std;
template <class T>
class stack_attack
{
private:
//Structure for node
struct stackNode
{
T value; //Node value
stackNode *next; //Pointer to next node
};
stackNode *top; //Pointer to stack top
public:
//Constructors
stack_attack()
{
top = NULL;
}
//Deconstructor
~stack_attack();
//Function Prototypes
bool push(T);
void pop(T&);
bool isEmpty();
};
//Deconstructor
template <class T>
stack_attack<T>::~stack_attack()
{
stackNode *nodePtr;
stackNode *nextNode;
//Set nodePtr to top of stack
nodePtr = top;
//Delete all nodes
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
//Push
template <class T>
bool stack_attack<T>::push(T item)
{
stackNode *newNode; //Pointer to new node
bool status;
//Allocate new node
newNode = new stackNode;
newNode->value = item;
//If there are no remaining nodes in stack
try
{
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
status = true;
cout << item << " pushed to stack. Bool Status: " << status << endl << endl;
}
else
{
newNode->next = top;
top = newNode;
status = true;
cout << item << " pushed to stack. Bool Status: " << status << endl << endl;
}
}
catch(exception e)
{
status = false;
cout << item << " Was NOT pushed to stack. Bool Status: " << status << endl << endl;
cout << e.what() << endl;
}
return status;
}
//Pop
template <class T>
void stack_attack<T>::pop(T &item)
{
stackNode *temp; //Temp node ptr
bool status;
try
{
//Check for empty stack
if (isEmpty())
{
cout << "ERROR: STACK EMPTY" << endl;
status = false;
cout << item << " Was NOT popped from stack. Bool Status: " << status << endl << endl;
}
else
{
item = top->value;
temp = top->next;
delete top;
top = temp;
status = true;
cout << item << " Was popped from stack. Bool Status: " << status << endl << endl;
}
}
catch(exception e)
{
status = false;
cout << item << " Was NOT popped from stack. Bool Status: " << status << endl << endl;
cout << e.what() << endl;
}
}
//Bool isEmpty
template <class T>
bool stack_attack<T>::isEmpty()
{
bool status;
if(!top)
{
status = true;
}
else
{
status = false;
}
return status;
}
InventoryBin类头文件和CPP文件
#ifndef __Inventory_Bin__InventoryBin__
#define __Inventory_Bin__InventoryBin__
#include <iostream>
#include <string>
using namespace std;
class InventoryBin
{
private:
int int_serialNum;
int int_lotNum;
string str_manufactDate;
public:
/*!
Constructor Prototype
!*/
InventoryBin();
InventoryBin(int, string,int);
/*!
Function Prototypes
!*/
int getSerialNum();
int getLotNum();
string getManufactDate();
void setSerialNum(int);
void setLotNum(int);
void setManufactDate(string);
friend ostream &operator << (ostream &stream, InventoryBin &obj);
};
#endif /* defined(__Inventory_Bin__InventoryBin__) */
#include "InventoryBin.h"
#include <iostream>
#include <string>
using namespace std;
/*!
Constructors
!*/
InventoryBin::InventoryBin()
{
int_serialNum = 0;
int_lotNum = 0;
str_manufactDate = "";
}
InventoryBin::InventoryBin(int sn, string date, int ln)
{
int_serialNum = sn;
str_manufactDate = date;
int_lotNum = ln;
}
/*!
Functions
!*/
//Getters
//Return int_serialNum value
int InventoryBin::getSerialNum()
{
return int_serialNum;
}
//Return str_manufactDate value
string InventoryBin::getManufactDate()
{
return str_manufactDate;
}
//Return int_lotNum value
int InventoryBin::getLotNum()
{
return int_lotNum;
}
//Setters
//Set int_serialNum value
void InventoryBin::setSerialNum(int sn)
{
int_serialNum = sn;
}
//Set str_manufactDate value
void InventoryBin::setManufactDate(string date)
{
str_manufactDate = date;
}
//Set int_lotNum value
void InventoryBin::setLotNum(int ln)
{
int_lotNum = ln;
}
ostream& operator << (ostream &stream, InventoryBin &obj)
{
stream << "Serial Number: " << &InventoryBin::getSerialNum << "\n" << "Manufactured Date: " << &InventoryBin::getManufactDate << "\n" << "Lot Number: " << &InventoryBin::getLotNum << endl << endl;
return stream;
}
主要
#include "stack_template.h"
#include "InventoryBin.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
/*!
Function Prototypes
!*/
void popItem(stack_attack<InventoryBin>&);
void pushItem(stack_attack<InventoryBin>&);
/*!
Main Application
!*/
int main()
{
//Variables
int int_counter = 0; //Hold # of database entries
int int_choice; //Menu Input
bool bl_exit = false; //If == true, will exit program
InventoryBin item;
stack_attack<InventoryBin> stack1;
while(!bl_exit)//Start switch case while loop
{
system("cls"); //House cleaning, wipe screen when returning to main menu :-)
cout << "*********************************************************" << endl;
cout << "******************* Inventory Database ******************" << endl;
cout << "*********************************************************" << endl;
cout << endl;
cout << endl;
cout << " 1. Enter Inventory into Database" << endl;
cout << endl;
cout << " 2. Remove Inventory from Database" << endl;
cout << endl;
cout << " 3. Show Datebase and then exit application" << endl;
cout << endl;
cout << "Go to: ";
cin >> int_choice;
//Switches
switch (int_choice)
{
case 1:
{
cout << "How many items do you want to enter into the database?: ";
cin >> int_counter;
cout << endl;
cout << endl;
for(int i = 0; i < int_counter; i++)
{
pushItem(stack1);
}
cout << "press any button to return to the main menu" << endl;
cin.get();
}
break;
case 2:
{
popItem(stack1);
}
break;
case 3:
{
try {
for (int i = 0; i < int_counter; i++)
{
stack1.pop(item);
}
return 0;
}
catch (exception e)
{
cout << "Stack is empty. " << e.what();
return 0;
}
}
}
}//End of switch case while loop
}
/*!
Functions
!*/
void popItem(stack_attack<InventoryBin>&stack2)
{
InventoryBin item;
stack2.pop(item);
}
void pushItem(stack_attack<InventoryBin>&stack2)
{
int int_serialNum1; //Hold serialnumber value
string manufactDate1; //Hold manufacturing date value
int int_lotNum1; //Hold lotNum value
cout << "Serial Number: ";
cin >> int_serialNum1;
cout << endl;
cout << "Manufacturing Date: ";
cin >> manufactDate1;
cout << endl;
cout << "Lot Number: ";
cin >> int_lotNum1;
InventoryBin myobj(int_serialNum1,manufactDate1,int_lotNum1);
stack2.push(myobj);
}
那么,你有没有在调试器中检查你的代码,看看发生了什么? – OldProgrammer
这是很多代码。你知道问题出在哪里吗?你已经做了什么来尝试隔离问题?我看到一堆调试输出,它在运行时会告诉你什么? –
你使用'std :: stack'(http://www.cplusplus.com/reference/stack/stack/)得到了不同的结果吗? – andand