2015-10-25 97 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