使用未声明的标识符';顶部';;你是说';流行音乐';? 我的教授给我一些C++的关于跳棋游戏回溯的代码。但它不会为我编译。我得到这个错误,使用未声明的标识符'top';你是说“pop”吗?如果需要,下面是代码的其余部分。谢谢 // 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; } //Maze01.cpp:定义控制台应用程序的入口点。 #包括 #包括 #包括 使用名称空间std; 模板 类堆栈:公共堆栈{ 公众: T pop(){ T tmp=top();//

使用未声明的标识符';顶部';;你是说';流行音乐';? 我的教授给我一些C++的关于跳棋游戏回溯的代码。但它不会为我编译。我得到这个错误,使用未声明的标识符'top';你是说“pop”吗?如果需要,下面是代码的其余部分。谢谢 // 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; } //Maze01.cpp:定义控制台应用程序的入口点。 #包括 #包括 #包括 使用名称空间std; 模板 类堆栈:公共堆栈{ 公众: T pop(){ T tmp=top();//,c++,backtracking,maze,boggle,C++,Backtracking,Maze,Boggle,通过以下方式进行修复: T tmp = this->top(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< T tmp=this->top();//编译器没有抱怨,而是抱怨top()?也许可以解释一下这个问题;-)你在用什么编译器?它可以用VS2013编译。g++。我的mac上的

通过以下方式进行修复:

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

T tmp=this->top();//编译器没有抱怨,而是抱怨
top()
?也许可以解释一下这个问题;-)你在用什么编译器?它可以用VS2013编译。g++。我的mac上的程序,我的教授用VS。我希望VS在mac上运行:(.如果我使用与VS相同的编译器,它会工作吗?记下你的指导老师在一行下面做了什么。做同样的事情并强制模板专门化。然后在文件顶部添加一个include for up和其余的include,因为这可能是你下一个问题的答案。咒语删除!我从来没有想过。更好的方法是这让它可以编译,但不能解决迷宫问题,它只是停止进程,让我退出运行的控制台it@AlexLopez我认为你比我更了解程序应该做什么。我认为你的下一步是单步通过代码找到问题。我知道程序应该做什么。我我以前用过它,我只是想在我的电脑上学习,谢谢你的帮助!如果我是你,我会接受这个建议。浏览代码,不管是否工作,对学习都很有帮助。
/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