2016-04-15 16 views
-1

好的,第一次使用stackoverflow,請和我一起裸照。使用未聲明的標識符'top';你的意思是'流行'嗎?

我的教授給了我一些關於boggle遊戲回溯的C++代碼。但它不會爲我編譯。我得到這個錯誤,使用未聲明的標識符'頂部';你的意思是'流行'嗎?並且如果需要的話,則代碼的其餘部分。謝謝!

// Maze01.cpp : Defines the entry point for the console application. 

#include <cstdlib> 
#include <iostream> 
#include <stack> 
using namespace std; 
template<class T> 
class Stack : public stack<T> { 
public: 
    T pop() { 
     T tmp = top(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
     stack<T>::pop(); 
     return tmp; 
    } 
}; 

class Cell { 
public: 
    Cell(int i = 0, int j = 0) { 
     x = i; y = j; 
    } 
    bool operator== (const Cell& c) const { 
     return x == c.x && y == c.y; 
    } 
private: 
    int x, y; 
    friend class Maze; 
}; 

class Maze { 
public: 
    Maze(); 
    void exitMaze(); 
private: 
    Cell currentCell, exitCell, entryCell; 
    const char exitMarker, entryMarker, visited, passage, wall; 
    Stack<Cell> mazeStack; 
    char **store;   // array of strings; 
    void pushUnvisited(int, int); 
    int rows, cols; 
    friend ostream& operator<< (ostream& out, const Maze& maze) { 
     for (int row = 0; row <= maze.rows + 1; row++) 
      out << maze.store[row] << endl; 
     out << endl; 
     return out; 
    } 
}; 

Maze::Maze() : exitMarker('e'), entryMarker('m'), visited('.'), 
passage('0'), wall('1') { 
    Stack<char*> mazeRows; 
    char str[80], *s; 
    int col, row = 0; 
    cout << "Enter a rectangular maze using the following " 
     << "characters:\nm - entry\ne - exit\n1 - wall\n0 - passage\n" 
     << "Enter one line at at time; end with Ctrl-z:\n"; 
    while (cin >> str) 
    { 
     row++; 
     cols = strlen(str); 
     s = new char[cols + 3]; // two more cells for borderline columns; 
     mazeRows.push(s); 
     strcpy(s + 1, str); 
     s[0] = s[cols + 1] = wall; // fill the borderline cells with 1s; 
     s[cols + 2] = '\0'; 
     if (strchr(s, exitMarker) != 0) 
     { 
      exitCell.x = row; 
      exitCell.y = strchr(s, exitMarker) - s; 
     } 
     if (strchr(s, entryMarker) != 0) 
     { 
      entryCell.x = row; 
      entryCell.y = strchr(s, entryMarker) - s; 
     } 
    } 
    rows = row; 
    store = new char*[rows + 2];  // create a 1D array of pointers; 
    store[0] = new char[cols + 3];  // a borderline row; 
    for (; !mazeRows.empty(); row--) { 
     store[row] = mazeRows.pop(); 
    } 
    store[rows + 1] = new char[cols + 3]; // another borderline row; 
    store[0][cols + 2] = store[rows + 1][cols + 2] = '\0'; 
    for (col = 0; col <= cols + 1; col++) { 
     store[0][col] = wall;   // fill the borderline rows with 1s; 
     store[rows + 1][col] = wall; 
    } 
} 

void Maze::pushUnvisited(int row, int col) { 
    if (store[row][col] == passage || store[row][col] == exitMarker) { 
     mazeStack.push(Cell(row, col)); 
    } 
} 
void Maze::exitMaze() { 
    int row, col; 
    currentCell = entryCell; 
    while (!(currentCell == exitCell)) { 
     row = currentCell.x; 
     col = currentCell.y; 
     cout << *this;   // print a snapshot; 
     if (!(currentCell == entryCell)) 
      store[row][col] = visited; 
     pushUnvisited(row - 1, col); 
     pushUnvisited(row + 1, col); 
     pushUnvisited(row, col - 1); 
     pushUnvisited(row, col + 1); 
     if (mazeStack.empty()) { 
      cout << *this; 
      cout << "Failure\n"; 
      return; 
     } 
     else currentCell = mazeStack.pop(); 
    } 
    cout << *this; 
    cout << "Success\n"; 
} 


int main(int argc, char* argv[]) 
{ 

    Maze().exitMaze(); 
    return 0; 
} 
+0

編譯器不抱怨['堆棧 :: pop()方法']( http://www.cplusplus.com/reference/stack/stack/pop/)但是關於'top()'?也許http://www.cplusplus.com/reference/stack/stack/top/可以解決這個問題;-) – VolkerK

+0

你使用什麼編譯器?它編譯與VS2013確定。 –

+0

g ++。我在我的Mac上編程,我的教授使用VS.我希望VS在mac上運行:(如果我使用與VS相同的編譯器,它會工作嗎? –

回答

1

修復這樣說:

T tmp = this->top(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

gcc5.3解釋了原因:

/tmp/gcc-explorer-compiler116315-75-7jbdnd/example.cpp: 
    In member function 'T Stack<T>::pop()': 
13 : error: there are no arguments to 'top' that depend on a template parameter, 
    so a declaration of 'top' must be available [-fpermissive] 
T tmp = top(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
^ 
13 : note: (if you use '-fpermissive', G++ will accept your code, 
    but allowing the use of an undeclared name is deprecated) 
Compilation failed 
+0

Expletive deleted!我從來沒有想到這一點,比我的音調更好 – user4581301

+0

這使得它可以編譯,但它不能解決迷宮它只是停止了這個過程並將我從運行它的控制檯中註銷 –

+2

@AlexLopez我認爲你比較瞭解程序應該做什麼比我更好,我認爲他的下一步是單步執行找到問題的代碼 –

相關問題