使用未声明的标识符“top”;您指的是 'pop' 吗?
Posted
技术标签:
【中文标题】使用未声明的标识符“top”;您指的是 \'pop\' 吗?【英文标题】:use of undeclared identifier 'top'; did you mean 'pop'?使用未声明的标识符“top”;您指的是 'pop' 吗? 【发布时间】:2016-04-15 00:14:14 【问题描述】:我的教授给了我一些关于回溯的 c++ 代码。但它不会为我编译。我收到此错误,使用未声明的标识符“top”;您的意思是“流行”吗?如果需要,这是其余的代码。谢谢!
// 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;
【问题讨论】:
编译器不会抱怨stack<T>::pop()
,而是抱怨top()
?也许cplusplus.com/reference/stack/stack/top 可以解决这个问题;-)
你用的是什么编译器?用VS2013编译OK。
g++。我在我的 Mac 上编程,我的教授使用 VS。我希望 VS 在 mac 上运行 :(。如果我使用与 VS 相同的编译器,它会工作吗?
记下你的导师做了什么。做同样的事情并强制模板专业化。然后在文件顶部添加一个包含 这样解决:
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
【讨论】:
脏话已删除!我什至从来没有想过这一点。比我的推销要好得多。 这允许它编译,但它不能解决迷宫它只是停止进程并将我退出运行它的控制台 @AlexLopez 我认为你比我更清楚程序应该做什么。我认为他的下一步是单步执行代码以找到问题。 我知道程序应该做什么。在我只是想在我的电脑上学习之前我已经使用过它,不过感谢您的帮助! 如果我是你,我会接受这个建议。浏览代码,无论是否工作,对学习都非常有帮助。以上是关于使用未声明的标识符“top”;您指的是 'pop' 吗?的主要内容,如果未能解决你的问题,请参考以下文章
Google iOS SDK 无法存档(使用未声明的标识符错误)
“错误 C2065:'ON_WM_THEMECHANGED':未声明的标识符”[关闭]