error : "list" does not name a type

I am writing a program to compute BFS of a directed graph using class. The program is scattered across three files. GraphClass.h , GraphClass.cpp and BFS.cpp. Whenever I try to compile the program , it give me an error mentioned in the title. i tried search for the solution and got link of this forum : http://www.cplusplus.com/forum/beginner/51696/

However, when i try adding the using namespace std; in GraphClass.h, it gives following output

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

/*******************GraphClass.h *******************************/
 /*
This file contains the declaration of the class and its memebres
http://stackoverflow.com/questions/12733888/regarding-c-include-another-class
Directed graphs
pastebin of this cod : http://pastebin.com/dgAgTq8D
*/

#include <list>
//using namespace std;
class Graph
{

	int v;
	list<int> *adj;			// pointer to array who contains list of ints
public:
	Graph(int v);				// This constructor is used to intilaize the graphs vertyices and arrau
	~Graph();
	void addEdge(int src,int dest);
	void BFS(int src);
};

/***********************GraphClass.cpp ************************/
/*
Tjis file contain definition of functions declared in GraphClass.h ( http://pastebin.com/dgAgTq8D )
This file's pastebin id : http://pastebin.com/atjfYmhT
*/

#include <iostream>
#include <queue>
#include "GraphClass.h"
#include <list>
#include <cstdbool>

Graph :: Graph(int v)
{
	this ->v = v;			// create graph's vertices as v
	adj = new list<int> [v];	// create an array of list of ints of size V
}

void Grpah:: addEdge(int src,int dest)
{
	// because it is defined as list, simple push back
	adj[src].push_back(dest);
}

void Graph:: BFS(int src)
{
	// create a boolean visited array dynamically using tyhe value of class member v and mark all as false initially
	bool *visited = new bool[v];
	int i;
	for (i = 0; i < v; ++i)
		visited[i] = false;
	// create a queue for pushing the children
	queue<int> q;
	// push the current node and make it visited as true
	q.push(src);
	visited [src] = true;
	// do until the queue is empty i.e. NODES visited
	while(!q.empty())
	{
		src = q.front();
		q.pop();
		cout<<src<<" "<<endl;
		for (std::list<int>::iterator i = adj[i].begin(); i != adj[i].end(); ++i)
		{
			if(visited[*i]== false)
			{
				visited[*i]= true;
				q.push(*i);
			}
		}
	}
}

/****************************BFS.cpp*******************************/
/*
See GraphClass.h (http://pastebin.com/dgAgTq8D)
and GraphClass.cpp (http://pastebin.com/atjfYmhT )
pastebin for this : http://pastebin.com/JwCwvdgC
*/

#include <iostream>
#include <queue>
#include <list>
#include <cstdbool>
#include "GraphClass.h"
using namespace std;

 int main(int argc, char const *argv[])
{
	// creating graph of 54 vertices
	Graph g(4);
	g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);
 
    cout << "Following is Breadth First Traversal (starting from vertex 2) \n";
    g.BFS(2);
	return 0;
}


Output : 1) with using "#include <list>" without using "using namespace std;"
in GraphClass.h

hduser@M-1939:~/Dropbox/ds/graph$ g++ -g -std=c++11 BFS.cpp -o BFS
In file included from BFS.cpp:5:0:
GraphClass.h:13:2: error: ‘list’ does not name a type
list<int> *adj; // pointer to array who contains list of ints

Output 2) With using "#include <list>" and with using "using namspace std;" in GraphClass.h

hduser@M-1939:~/Dropbox/ds/graph$ g++ -g -std=c++11 BFS.cpp -o BFS
/tmp/ccmce0nw.o: In function `main':
/home/hduser/Dropbox/ds/graph/BFS.cpp:11: undefined reference to `Graph::Graph(int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:12: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:13: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:14: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:15: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:16: undefined reference to `Graph::addEdge(int, int)'
/tmp/ccmce0nw.o:/home/hduser/Dropbox/ds/graph/BFS.cpp:17: more undefined references to `Graph::addEdge(int, int)' follow
/tmp/ccmce0nw.o: In function `main':
/home/hduser/Dropbox/ds/graph/BFS.cpp:20: undefined reference to `Graph::BFS(int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:21: undefined reference to `Graph::~Graph()'
/home/hduser/Dropbox/ds/graph/BFS.cpp:21: undefined reference to `Graph::~Graph()'
collect2: error: ld returned 1 exit status
hduser@M-1939:~/Dropbox/ds/graph$ g++ -g -std=c++11 BFS.cpp -o BFS
/tmp/cc08MGZc.o: In function `main':
/home/hduser/Dropbox/ds/graph/BFS.cpp:17: undefined reference to `Graph::Graph(int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:18: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:19: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:20: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:21: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:22: undefined reference to `Graph::addEdge(int, int)'
/tmp/cc08MGZc.o:/home/hduser/Dropbox/ds/graph/BFS.cpp:23: more undefined references to `Graph::addEdge(int, int)' follow
/tmp/cc08MGZc.o: In function `main':
/home/hduser/Dropbox/ds/graph/BFS.cpp:26: undefined reference to `Graph::BFS(int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:27: undefined reference to `Graph::~Graph()'
/home/hduser/Dropbox/ds/graph/BFS.cpp:27: undefined reference to `Graph::~Graph()'
collect2: error: ld returned 1 exit status
void Grpah:: addEdge(int src,int dest)

You spelt Graph wrong. Hence it's moaning about addEdge().
@mutexe
Thanks for reporting that typo, Corrected it. Still problem persists.

hduser@M-1939:~/Dropbox/ds/graph$ g++ -g -std=c++11 BFS.cpp -o BFS
/tmp/ccuFJ6As.o: In function `main':
/home/hduser/Dropbox/ds/graph/BFS.cpp:17: undefined reference to `Graph::Graph(int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:18: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:19: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:20: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:21: undefined reference to `Graph::addEdge(int, int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:22: undefined reference to `Graph::addEdge(int, int)'
/tmp/ccuFJ6As.o:/home/hduser/Dropbox/ds/graph/BFS.cpp:23: more undefined references to `Graph::addEdge(int, int)' follow
/tmp/ccuFJ6As.o: In function `main':
/home/hduser/Dropbox/ds/graph/BFS.cpp:26: undefined reference to `Graph::BFS(int)'
/home/hduser/Dropbox/ds/graph/BFS.cpp:27: undefined reference to `Graph::~Graph()'
/home/hduser/Dropbox/ds/graph/BFS.cpp:27: undefined reference to `Graph::~Graph()'
collect2: error: ld returned 1 exit status
Last edited on
@mutexe
got the error. I was only compiling BFS.cpp while leaving GraphClass.cpp
Compiled both together like
" g++ -g -std=c++11 BFS.cpp GraphClass.cpp -o BFS"
and run like
"./BFS"
got correct answer.
cool.
Topic archived. No new replies allowed.