Когда я извлекаю элемент из моего стека шаблонов и печатаю извлеченное значение, это всегда число 1. Оно всегда возвращает 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;
//Switchesswitch (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 valuecout << "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);
}
Измените свой перегруженный operator <<
оператор с кодом ниже:
ostream& operator << (ostream &stream, InventoryBin &obj)
{
stream << "Serial Number: " << obj.getSerialNum() << "\n" << "Manufactured Date: " << obj.getManufactDate() << "\n" << "Lot Number: " << obj.getLotNum() << endl << endl;
return stream;
}
Причина: Вы все испортили в operator <<
, Вы пытались напечатать &InventoryBin::getSerialNum
, &InventoryBin::getManufactDate
а также &InventoryBin::getLotNum
вместо вызова соответствующих функций-членов ввода obj
,
Объяснение того, почему адрес функции печатается как 1 в C ++, @ Приветствия и hth. Альф
В стандартном C ++ указатели на функции, включая указатели на членов класса
функции, не преобразуются вvoid*
, а скорееbool
,
Следовательно, вместо ожидаемого адреса функции печатается «1».
stream << "Serial Number: " << &InventoryBin::getSerialNum << "\n" << "Manufactured Date: " << &InventoryBin::getManufactDate << "\n" << "Lot Number: " << &InventoryBin::getLotNum << endl << endl;
Почему вы печатаете адреса функций, а не объекта или его свойств?