Franken Frank
Franken Frank

Reputation: 85

How to fix 'C2061 syntax error identifier 'stack'' in visual studio?

I'm using stack in my code and it shows this error. Other questions say I have circular dependency but I only have 1 header file

//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);    
}

It should run but it keeps giving me this syntax error: "error C2061: syntax error : identifier 'stack' "

Upvotes: 0

Views: 1691

Answers (1)

john
john

Reputation: 87962

Your header file should look like this

//function.h
#pragma once 

#include <stack>

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

You need to #include <stack> before the dfs function prototype is seen.

You should fully qualify the type name stack (i.e. std::stack not stack) because using namespace std; is a very bad idea in a header file.

Upvotes: 2

Related Questions