如何在Visual Studio中修复'C2061语法错误标识符'堆栈'?

时间:2019-02-15 09:13:00

标签: c++ syntax

我在代码中使用堆栈,它显示了此错误。其他问题说我有循环依赖关系,但我只有1个头文件

//function.h
#pragma once 

void dfs(int start, int goal, bool visited[], int **matrix, int size,
         bool &found, stack<int>& path);
//function.cpp
#include "function.h"
#include <iostream>
#include <stack>

using namespace std;

void dfs(int start, int goal, bool visited[], int **matrix, int size,
         bool &found, stack<int>& path)
{
    visited[start] = true;
    cout<<start<<" ";
    path.push(start);

    if (start == goal)
        found = true;

    for (int k = 0; k < size; k++)
    {
        if (visited[k] == false && matrix[start][k] && found == false )
            dfs(k,goal,visited,matrix,size,found,path);
        path.pop();
    }
}
//main.cpp
#include "function.h"
#include <iostream>
#include <fstream>
#include <stack>

using namespace std;
void main()
{
    stack<int> path;

    for(int i=0; i<N; i++)
        visit[i] = false; //init visit

    for(int i = 0; i < N; ++i)
        matrix[i] = new int[N]; // build rows

    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < N; ++j)
        {
            fin>>matrix[i][j];
        }
    }

    dfs(start, end, visit, matrix, 5, found, path);    
}

它应该运行,但是会不断给我这个语法错误:“错误C2061:语法错误:标识符'stack'”

1 个答案:

答案 0 :(得分:2)

您的头文件应该看起来像这样

//function.h
#pragma once 

#include <stack>

void dfs (int start, int goal, bool visited[], int **matrix, int size, bool 
&found, std::stack<int>& path);

您需要先#include <stack>,然后才能看到dfs函数原型。

您应该完全限定类型名称stack(即std::stack而不是stack),因为using namespace std;是头文件中的very bad idea