我正在尝试打印堆栈的内容。 这是我的代码: Stack.h
#pragma once
#include <iostream>
using std::cout; // specific commands from namespace std
using std::cin;
using std::endl;
typedef unsigned long Item;
class Stack
{
private:
enum { MAX = 10 }; // MAX elements in this stack
Item items[MAX]; // holds stack items
int top; // index for the top stack item
int first, last;
public:
Stack();
Stack(int, int);
~Stack();
void printCurr() const;
void printCurrReverse() const;
bool isempty() const;
bool isfull() const;
// return false if stack already full
bool push(const Item& item); // add item to stack
// return false if stack already empty
bool pop(Item& item);
};
stack.cpp
#pragma once
#include "Stack.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
Stack::Stack()
{
for (int i = 0; i < MAX; i++) //for loop that sets the initial array values to null
{
items[i] = 0;
}
top = 0;
}
Stack::Stack(int first , int last)
{
if (top <= 4)
{
for (int i = 0; i <= 4; i++)
{
this -> items[i] = first;
}
}
if (top >= 5 && top <= MAX)
{
for (int i = 0; i <= MAX; i++)
{
this -> items[i] = last;
}
}
}
Stack::~Stack()
{
}
void Stack::printCurr() const
{
int index = 0;
for (int i = index; i < MAX; i++)
{
cout << index + i;
}
}
void Stack::printCurrReverse() const
{
int index = top - 1;
for (int i = index; i >= 0; i++)
{
cout << index + i;
}
}
bool Stack::isempty() const
{
return top == 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::push(const Item& item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
bool Stack::pop(Item& item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}
的main.cpp
#include <iostream>
#include <sstream>
#include "Stack.h"
using namespace std;
int main()
{
Stack st1;
char ch;
unsigned long sc;
cout << "Please Enter 'A' to Add A Score You Wish To Record, \n"
<< "Press R To Record A Score, V To View Recorded Scores & Q to Quit" << endl;
while (cin >> ch && toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch (ch)
{
case 'a':
case 'A': cout << "Enter A Score To Add: ";
cin >> sc;
if (st1.isfull())
cout << "stack already full\n";
else
st1.push(sc);
break;
case 'R':
case 'r': if (st1.isempty())
cout << "stack already empty\n";
else {
st1.pop(sc);
cout << "Score #" << sc << " popped\n";
}
break;
case 'V':
case 'v':
cout << "Your Recorded Scores are : " << st1.printCurr() << endl;
cout << "Your Recorded Scores In Reverse Order Are : " << st1.printCurrReverse() << endl;
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";
}
system("pause");
return 0;
}
}
我的问题是打印功能。我收到以下错误
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
main.cpp 48
根据我的理解,这种情况正在发生,因为我没有正确地重载<<
运算符。
我需要打印功能不带参数且没有返回值。 我做错了什么?
答案 0 :(得分:2)
您的打印功能copy
和printCurr
返回无效,因此无法用作printCurrReverse
的参数(在std::cout
中)。你的功能已经用cout打印出来。
您可以让函数返回字符串。或者从main
行中删除它们。
最简单的方法是将它们排除在cout线之外:
cout
否则,让您的打印功能返回cout << "Your Recorded Scores are : ";
st1.printCurr();
cout << "Your Recorded Scores In Reverse Order Are : ";
st1.printCurrReverse();
,您可以使用string
创建该代码:
stringstream
答案 1 :(得分:0)
首先是这个构造函数
Stack::Stack(int first , int last)
{
if (top <= 4)
{
for (int i = 0; i <= 4; i++)
{
this -> items[i] = first;
}
}
if (top >= 5 && top <= MAX)
{
for (int i = 0; i <= MAX; i++)
{
this -> items[i] = last;
}
}
}
没有意义并且具有未定义的行为,因为数据成员top未初始化。目前尚不清楚你要做什么。
功能printCurr
也没有意义
void Stack::printCurr() const
{
int index = 0;
for (int i = index; i < MAX; i++)
{
cout << index + i;
}
}
它只输出[0, MAX - 1]
如果您要输出存储在堆栈中的值,则该函数可以按以下方式显示
void Stack::printCurr() const
{
for ( int i = top; i != 0; )
{
cout << items[--i];
}
}
相应的函数printCurrReverse
可能看起来像
void Stack::printCurrReverse() const
{
for ( int i = 0; i != top ; i++)
{
cout << items[i];
}
}
否则函数的返回类型为void,您可能不会像
那样使用它们cout << "Your Recorded Scores In Reverse Order Are : " << st1.printCurrReverse() << endl;
如果你想以这种方式使用它们,那么它们应该被定义为
std::ostream & Stack::printCurr( std::ostream &os = std::cout ) const
{
for ( int i = top; i != 0; )
{
os << items[--i];
}
return os;
}
和
std::ostream & Stack::printCurrReverse( std::ostream &os = std::cout ) const
{
for ( int i = 0; i != top ; i++)
{
os << items[i];
}
return os;
}