2015-10-25 72 views
0

我正在尝试打印堆栈的内容。 这里是我的代码: Stack.h堆栈C++的打印功能

#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 

我需要打印功能与出参数和没有返回值。 我在做什么错?

+1

什么是实际的错误信息? – melpomene

+0

严重性\t \t代码描述\t \t项目文件\t线 错误\t \t C2679二进制“<<”:没有操作员发现它采用类型的“无效”(或没有可接受的转换)一个右边的操作数 – Jpoor

回答

2

您的打印功能printCurrprintCurrReverse返回void,因此不能用作参数std::cout(在您的main中)。你的功能已经用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(); 
} 
+2

或者干脆去掉'cout <<'部分。 – melpomene

+0

好的,有我能看到的例子吗? – Jpoor

+1

@Jpoor'st1.printCurr();' – melpomene

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如果你想使用它们这样一种方式,他们应该像

std::ostream & Stack::printCurr(std::ostream &os = std::cout) const 
{ 
    for (int i = top; i != 0;) 
    { 
     os << items[--i]; 
    } 

    return os; 
} 

和定义,你可能无法使用他们喜欢

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

std::ostream & Stack::printCurrReverse(std::ostream &os = std::cout) const 
{ 
    for (int i = 0; i != top ; i++) 
    { 
     os << items[i]; 
    } 

    return os; 
} 
+0

对于第一个函数,我试图将堆栈分成两个独立的整数。第一个int包含堆栈的前五个元素,第二个int包含堆栈的最后五个元素。 – Jpoor

+0

@Jpoor在任何情况下,它都不清楚你在做什么以及你将要做什么。:) –

+0

我想告诉它首先为int的前五个元素初始化int,然后为int堆栈中的下五个元素... – Jpoor