Recently, I am learning C++. I am trying to print the adjacencyNodes but it shows error and I can't understand why
Here is the code
Class for List
#include <iostream>
using namespace std;
class List{
int list[100];
int size = 0;
public:
void add(int a){
list[size] = a;
size++;
}
int get(int i){
return list[i];
}
};
Class for Graph
class Graph{
bool isDirected;
int noNodes;
int adjacencyMatrix[100][100];
int noEdges = 0;
public:
Graph(bool isDirected, int noNodes){
this->isDirected = isDirected;
this->noNodes = noNodes;
for(int i=0;i<noNodes;i++)
for(int j=0;j<noNodes;j++)
adjacencyMatrix[i][j]=0;
}
void addEdge(int a, int b){
adjacencyMatrix[a][b] = 1;
if (!isDirected) adjacencyMatrix[b][a] = 1;
noEdges++;
}
bool isEdge(int a, int b){
if(adjacencyMatrix[a][b] == 1) return 1;
else return 0;
}
int inDegree(int a){
int indegree=0;
for(int m=0;m<noNodes;m++){
if(adjacencyMatrix[m][a] == 1){
indegree++;
}
}
return indegree;
}
int outDegree(int a){
int outdegree=0;
for(int n =0;n < noNodes;n++){
if(adjacencyMatrix[a][n] == 1){
outdegree++;
}
}
return outdegree;
}
List adjacencyNodes(int a){
List adj;
int ind = 0;
for(int i=0; i<noNodes; i++){
if(adjacencyMatrix[a][i] == 1){
adj.add(i);
ind++;
}
}
return adj;
}
};
int main(){
int n, e, a, b, x;
//input number of nodes and number of Edges
cin>>n>>e;
//create a directed graph
Graph G(1,n);
for(int i=0;i<e;i++){
cin>>a>>b;
G.addEdge(a,b);
}
//input another number x
cin>>x;
//print the degree of x
cout<<"Indegree\n"<<G.inDegree(x);
cout<<"\nOutdegree\n"<<G.outDegree(x);
// print all the adjacent nodes of x
// List L = G.adjacencyNodes(x); for()
cout<<"\n"<<G.adjacencyNodes(x);
return 0;
}
I'm getting the error here
cout<<"\n"<<G.adjacencyNodes(x);
error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'List')
Is there any way to print it out?
operator<<
defined for yourList
class. You need to implement one. – Remy LebeauList
class, you had to overload another one:std::ostream& operator<<(std::ostream&, const List&)
. FYI: Overloading input/output operators in C++ – Scheff's Cat