gpt4 book ai didi

c++ - 堆栈 C++ 的打印函数

转载 作者:行者123 更新时间:2023-11-28 02:18:24 24 4
gpt4 key购买 nike

我正在尝试打印堆栈的内容。这是我的代码:栈.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);

};

堆栈.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;
}

主要.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

据我所知,这是因为我没有重载 <<运算符正确。

我需要打印函数没有参数且没有返回值。我做错了什么?

最佳答案

您的打印函数 printCurrprintCurrReverse 返回 void,因此不能用作 std::cout 的参数(在您的 主要)。你的函数已经用 cout 打印出来了。

您可以让您的函数返回字符串。或者从 cout 行中删除它们。

最简单的方法,将它们放在 cout 行之外:

cout << "Your Recorded Scores are : ";
st1.printCurr();
cout << "Your Recorded Scores In Reverse Order Are : ";
st1.printCurrReverse();

否则让你的打印函数返回一个 string,你可以用 stringstream 创建它,例如:

std::string printCurrReverse() {
stringstream strs;
strs << " text "<< 33; //example
return strs.str();
}

关于c++ - 堆栈 C++ 的打印函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33330960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com