1

I am trying to make this depth first search work, but I keep getting this weird error:

'inputEdges' must be initialized by constructor, not by '{---}'

I have no idea how to fix it.

/*
 * Adjancency List
*/
#include <iostream>
 #include <cstdlib>
 #include <bits/stdc++.h>
  using namespace std;

 vector<int> edges[5];
     bool visited[5];



 void dfs(int x)
{
visited[x] = true;
for(int i=0; i < edges[x].size(); i++)
    if(!visited[edges[x][i]])
        dfs(edges[x][i]);


}



/*
* Main function
*/
  int main()
 {


for(int i=0; i < 12; i++)
    visited[i] = false;
vector<pair<int, int> > inputEdges{{0, 1}, {0, 3}, {1, 2}, {1, 3}, {2, 4}, {2, 3}, {4, 5}, {5, 6}, {5, 1}, {3, 9}, {8, 7}, {7, 0}, {9, 1}};
for(int i=0; i < inputEdges.size(); i++)
{
    edges[inputEdges[i].first].push_back(inputEdges[i].second);
    edges[inputEdges[i].second].push_back(inputEdges[i].first);
}

dfs(0);
return 0;
}
Wesley Bland
  • 8,816
  • 3
  • 44
  • 59
  • Are you compiling with C++11? Anyway, you shouldn't be including ``. – chris Mar 05 '15 at 23:37
  • 3
    A loop like `for(int i=0; i < 12; i++)` used to index an array only declared as `bool visited[5];` is a recipe for undefined behavior. – WhozCraig Mar 05 '15 at 23:38
  • @chris [see here](http://stackoverflow.com/questions/25311011/how-does-include-bits-stdc-h-work-in-c) – M.M Mar 06 '15 at 00:51
  • @MattMcNabb, Interesting, I saw a proposal idea pop up in std-proposals about such a header. – chris Mar 06 '15 at 00:52
  • when i dont use i get an error vector does not name a type and ohh yeah I didnt notice the array thing when i was pasting this in....I was trying with different number of edges. I changed it to 12 and still get the same error.... Any ideas guys ? – Jacob13000x Mar 06 '15 at 00:56
  • use `#include ` to get vector – M.M Mar 07 '15 at 01:20

1 Answers1

1

Listing a vector's contents via brace-enclosed list is only valid in C++11 or later. You will need to use a compiler that complies with C++11 to compile this code.

M.M
  • 138,810
  • 21
  • 208
  • 365