Been Having a hard time with arrays

Write your question here.
Hi im coding graphs and it keeps on saying its not constant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  Pu#pragma once
#include <list>
#include <iostream>
#include <vector>
using namespace std;


class Graph
{
private:

  int **adj2;
  list <int> *adj;						           //Programmer2
	void DFSUtil(int v, bool visited[]);	 
	void BFSUtil(int , bool visited[]);	 //Lead

 public:
 
  const static  int V;
	vector<pair<int,int> > adj[V];
	Graph(int);								        //All	
	void add_edge(vector <pair<int, int> > adj[],int u,int v ,int wt);			
	void printGraph(vector<pair<int,int> > adj[], int V);						    //All
	void printGraph2();						    //Programmer2
	void DFS(int v);						      //Lead
	void BFS(int s);						      //Lead
  bool isReachable(int s,int d);
  int printShortestPath(int parent[], int s, int d);
  int findShortestPath(int src, int dest);
  void addEdge2(int u, int v);
};t the code you need help with here.

that is the data .h

Been Having a hard time with arrays

Do you use that as the title of all your posts?


keeps on saying its not constant

What is "it"?


that is the data .h

Quite possibly. But where is the rest of the code?

Your line 20 doesn't know what the value of V is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
using namespace std;

class Graph
{
public:
    const static int V = 5;
    vector<pair<int,int> > adj[V];
};


int main()
{
    Graph graph;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <vector>
#include "Data.h"
using namespace std;

int main()
{
	int ch;
  char ch2;
  int src;
  int dest;
  const int C = 5;
	Graph g(5);
  vector<pair<int,int> > adj[C];
	cout << "  Graph Operations" << endl;
	cout << "   [1] Create Graph" << endl;
	cout << "   [2] Traversal" << endl;
  cout << "   [3] Find Path" << endl;
	cout << "   [4] Path Cost" << endl;
  cout << "   [5] Exit Program" << endl;
	cout << "   [6] Main Program" << endl;
	cout << "      Enter choice: ";
	cin >> ch;
//[1] Create Graph
  if(ch == 1){
    cout << "Create Graph" << endl;
    cout << "[a] Enter name of places" << endl;
    cout << "[b] Adjacency List" << endl;
    cout << "[c] Adjacency Matrix" << endl;
    cout << "Enter choice: ";
    cin >> ch2;
  //[a] Adjacency List
  	if(ch2 == 'a')
  	{
  	cout<<" Enter How many places you want to enter: ";
	int a;
	cin>>a;
	string places[a];
	for(int i=0; i<a; i++)
	{
	cout<<" Place "<<i+1<<" :";
	cin>>places[i];
		}		
	  }
    else if (ch2 == 'b')
    {
       g.add_edge(adj,0,1,10);
       g.add_edge(adj,1,2,20);
       g.add_edge(adj,2,3,30);
       g.add_edge(adj,1,3,40);
       g.add_edge(adj,2,4,100);
       g.add_edge(adj,4,0,10);
       g.printGraph(adj,C);
      cout << endl << endl;
  //[b] Adjacency Matrix
    }
    else if (ch2 == 'c'){
       g.addEdge2(0,1);
       g.addEdge2(1,2);
       g.addEdge2(2,3);
       g.addEdge2(1,3);
       g.addEdge2(2,4);
       g.addEdge2(4,0);;
       g.printGraph2();
    }
  }
//[2] Traversal
  else if(ch == 2){
    cout << "Travesal"<< endl;
    cout << "[a] BFS" << endl;
    cout << "[b] DFS" << endl;
    cout << "Enter choice: ";
    cin >> ch2;
  //[a] BFS
    if(ch2 == 'a'){
      cout << "BFS";
      cout << endl << "BFS Traversal..." << endl;
      g.BFS(3);
  //[b] DFS
    }else if(ch2 == 'b'){
      cout << "DFS";
      cout << endl << "BFS Traversal..." << endl;
      g.BFS(3);
        cout << endl << "DFS Traversal..." << endl;
      g.DFS(1);
    }else{
      cout << "incorrect input";
      }
//[5] Find Path
  }else if (ch == 3){
    cout << "Find Path"<< endl;
    
    
    
    
    
    
//[4] Path Cost 
  }else if (ch == 4){
    cout << "Path Cost"<< endl;
    cout << "Enter the starting point: ";
    cin>>src;
    cout<< "Enter destination: ";
    cin>>dest;
    
    
    
//[5] Exit Program    
  }else if (ch == 5){
    cout << "Program has been exited"<< endl;
    exit(0);
}
}

This is my main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <list>
#include "Data.h"
#include <vector>
#include <queue>
using namespace std;


Graph::Graph(int x)
{
  
   vector<pair<int, int> > adj[V];
}
void add_edge(vector <pair<int, int> > adj[], int u, int v, int wt) 
{ 
  
    adj[u].push_back(make_pair(v, wt)); 
    adj[v].push_back(make_pair(u, wt)); 
} 
void Graph::addEdge2(int u, int v)
{
	adj2[u][v] = 1;
}
// A utility function to print the adjacency list 
// representation of graph 
void Graph::printGraph(vector<pair<int,int> > adj[], int V) 
{ 
    int v, w; 
    for (int u = 0; u < V; u++) 
    { 
        cout << "Node " << u << "->"; 
        for (auto it = adj[u].begin(); it!=adj[u].end(); it++) 
        { 
            v = it->first; 
            w = it->second; 
            cout << v <<endl ; 
        } 
        cout << ""; 
    } 
} 


void Graph::printGraph2()
{
	cout << "Adjacency Matrix..." << endl << endl;
	cout << "\t";
	for (int i = 0; i < V; i++)
		cout << "V[" << i << "]" << "\t";
	cout << endl;
	for (int i=0; i<V; i++)
	{
		cout << "V[" << i << "]" << "\t";
		for (int j = 0; j < V; j++)
			cout << adj2[i][j] << "\t";
		cout << endl;
	}
	cout << endl;
}

void Graph::DFSUtil(int v, bool visited[])
{
	// Mark the current node as visited and 
	// print it 
	visited[v] = true;
	cout << v << " ";

	// Recur for all the vertices adjacent 
	// to this vertex 
	list<int>::iterator i;
	for (i = adj[v].begin(); i != adj[v].end(); ++i)
		if (!visited[*i])
			DFSUtil(*i, visited);
}

// DFS traversal of the vertices reachable from v. 
// It uses recursive DFSUtil() 
void Graph::DFS(int v)
{
	// Mark all the vertices as not visited 
	bool *visited = new bool[V];
	for (int i = 0; i < V; i++)
		visited[i] = false;

	// Call the recursive helper function 
	// to print DFS traversal 
	
	DFSUtil(v, visited);
	for(int i=0; i< V; i++)
		if (!visited[i])
			DFSUtil(i, visited);

	for (int i = 0; i < V; i++)
		if (!visited[i])
			cout << i << " ";
}

void Graph::BFS(int s)
{
	// Mark all the vertices as not visited 
	bool *visited = new bool[V];
	for (int i = 0; i < V; i++)
		visited[i] = false;

	BFSUtil(s, visited);
	for (int i = 0; i < V; i++)
		if (!visited[i])
			BFSUtil(i, visited);

	for (int i = 0; i < V; i++)
		if (!visited[i])
			cout << i << " ";
}

void Graph::BFSUtil(int s, bool visited[])
{
	// Create a queue for BFS 
	list<int> queue;

	// Mark the current node as visited and enqueue it 
	visited[s] = true;
	queue.push_back(s);

	// 'i' will be used to get all adjacent 
	// vertices of a vertex 
	list<int>::iterator i;

	while (!queue.empty())
	{
		// Dequeue a vertex from queue and print it 
		s = queue.front();
		cout << s << " ";
		queue.pop_front();

		// Get all adjacent vertices of the dequeued 
		// vertex s. If a adjacent has not been visited,  
		// then mark it visited and enqueue it 
		for (i = adj[s].begin(); i != adj[s].end(); ++i)
		{
			if (!visited[*i])
			{
				visited[*i] = true;
				queue.push_back(*i);
			}
		}
	}
}



bool Graph::isReachable(int s,int d)
{
    // Base case
    if(s == d)
        return true;
 
    int n= (int)adj->size();
     
    // Mark all the vertices as not visited
    vector<bool> visited(n,false);
 
    // Create a queue for BFS
    queue<int> q;
    visited[s]= true;
    q.push(s);
 
    while(!q.empty())
    {
        // Dequeue a vertex from queue and print it
        s=q.front();
        q.pop();
 
        // Get all adjacent vertices of the dequeued vertex s
        // If a adjacent has not been visited, then mark it
        // visited  and enqueue it       
        for(auto x:adj[s])
        {
 
            // If this adjacent node is the destination node,
            // then return true
            if(x == d)
                return true;
 
            // Else, continue to do BFS           
            if(!visited[x])
            {
                visited[x] = true;
                q.push(x);
            }
        }
    }
 
 // If BFS is complete without visiting d
    return false;
}
 



//////////////////////////////////////////////////////////
// To print the shortest path stored in parent[]
int Graph::printShortestPath(int parent[], int s, int d)
{
    static int level = 0;
  
    // If we reached root of shortest path tree

    
    if (parent[s] == -1)
    {
        cout << "Shortest Path between " << s << " and "
             << d << " is "  << s << " ";
        return level;
    }
  
    printShortestPath(parent, parent[s], d);
  
    level++;
    if (s < V)
        cout << s << " ";
  
    return level;
}

// This function mainly does BFS and prints the
// shortest path from src to dest. It is assumed
// that weight of every edge is 1
int Graph::findShortestPath(int src, int dest)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[2*V];
    int *parent = new int[2*V];
  
    // Initialize parent[] and visited[]
    for (int i = 0; i < 2*V; i++)
    {
        visited[i] = false;
        parent[i] = -1;
    }
  
    // Create a queue for BFS
    list<int> queue;
  
    // Mark the current node as visited and enqueue it
    visited[src] = true;
    queue.push_back(src);
  
    // 'i' will be used to get all adjacent vertices of a vertex
    list<int>::iterator i;
  
    while (!queue.empty())
    {
        // Dequeue a vertex from queue and print it
        int s = queue.front();
  
        if (s == dest)
            return printShortestPath(parent, s, dest);
  
        queue.pop_front();
  
        // Get all adjacent vertices of the dequeued vertex s
        // If a adjacent has not been visited, then mark it
        // visited and enqueue it
        
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
                parent[*i] = s;
            }
        }
    }
}

and implementation
and it keeps on making errors
I believe I have diagnosed your primary issue. Please correct me if I'm wrong.

And post the exact errors you receive, verbatim.
Last edited on
In file included from Implementation.cpp:3:0:
Data.h:20:30: error: array bound is not an integer constant before ‘]’ token
vector<pair<int,int> > adj[V];
^
In file included from Main.cpp:3:0:
Data.h:20:30: error: array bound is not an integer constant before ‘]’ token
vector<pair<int,int> > adj[V];
this one
Yes, what happens when you assign a proper value to V?

Other note:
1
2
3
4
5
Graph::Graph(int x)
{
  
   vector<pair<int, int> > adj[V];
}

In your constructor, adj is a local variable, which has nothing to do with the variable you declared in your class.
Last edited on
In file included from Implementation.cpp:3:0:
Data.h:20:30: error: redeclaration of ‘std::vector<std::pair<int, int> > Graph::adj [5]’
vector<pair<int,int> > adj[V];
^
Data.h:13:15: note: previous declaration ‘std::__cxx11::list<int>* Graph::adj’
list <int> *adj; //Programmer2
^~~
In file included from Main.cpp:3:0:
Data.h:20:30: error: redeclaration of ‘std::vector<std::pair<int, int> > Graph::adj [5]’
vector<pair<int,int> > adj[V];
^
Data.h:13:15: note: previous declaration ‘std::__cxx11::list<int>* Graph::adj’
list <int> *adj; //Programmer2
^~~
this happens
Data.h:20:30: error: redeclaration of ‘std::vector<std::pair<int, int> > Graph::adj [5]’
vector<pair<int,int> > adj[V];


You have two different variables in your class scope both called 'adj'. This is not allowed.

Lines 13 and 20.
Last edited on
I tried removing the other one and it have a lot of errors
This is why you shouldn't try to write dozens/hundreds of lines of codes at once. You have errors on top of errors. Keep iterating on them and then deal with the next error, and so on.

Compile early, compile often. Aim small, miss small.
Last edited on
Can I ask what should I do to make it free of errors?
Sure you can ask. But I'm not very friendly, maybe somebody else will feel like going through hundreds of lines of code.

Fix one error, move onto the next one. Eventually something will improve. Save backups of your code in case something goes wrong.
Last edited on
No prob thanks for the tips tho!
https://www.geeksforgeeks.org/arrays-in-c-cpp/ : Please check the following link to learn more about arrays in C++.
Topic archived. No new replies allowed.