使用带有函数,类和堆栈的指针

时间:2014-02-15 00:15:37

标签: c++ function templates pointers stack

#include "InventoryBin.h"
#include "Stack.h"
#include <string>
#include <iostream>
using namespace std;

// Constants for the menu choices
const int ENTER_ITEM = 1,
  REMOVE_ITEM = 2,
  QUIT = 3;

int choice;      // To hold menu choice

// Function prototypes
void popItem(Stack<InventoryBin> *);
void pushItem(Stack<InventoryBin> *);
int menu();

void main()
{
  // Create a stack object
  Stack<InventoryBin> stack;

  // Use a loop to ask user for input
  do
  {
    menu();
    if (choice != QUIT)
    {
      switch (choice)
      {
        case ENTER_ITEM:
          pushItem(&stack);
          break;
        case REMOVE_ITEM:
          popItem(&stack);
      }
    }
  } while (choice != QUIT);
}
int menu()
{
  // Display menu
  cout << "Please Select\n"
     << ENTER_ITEM << " - Enter an item.\n"
     << REMOVE_ITEM << " - Remove an item.\n"
     << QUIT << " - Quit.\n";
  cin >> choice;

  // Validate
  while (choice < ENTER_ITEM || choice > QUIT)
  {
    cout << "Enter a valid choice: ";
    cin >> choice;
  }
  return choice;
}
void pushItem(Stack<InventoryBin> *stack)
{
  cout << "Enter serial number: ";
  cin >> stack;
  stack->push(stack);
  cin.ignore();
  cout << "Enter manufacture date: ";
  getline(cin, stack);
  stack->push(stack);
  cout << "Enter lot number: ";
  cin >> stack;
  stack->push(stack);
}

我正在尝试使pushItem和popItem工作。该程序创建一个数据类型为InventoryBin的堆栈...没有发布InventoryBin你知道我做错了什么吗?我的pushItem函数有错误代码。

没有合适的构造函数可以从StackInventoryBin *转换为InventoryBin

1 个答案:

答案 0 :(得分:1)

stack->push(stack);错了。查看stack::push。您应该在堆栈上推送ITEM,而不是整个堆栈。错误代码说明了所有内容:push()需要一个InventoryBin,但是您正在传递整个堆栈。

而且,我不知道如果你把整个堆叠推到自己身上会发生什么......