【发布时间】:2020-07-15 07:17:08
【问题描述】:
最近,我正在学习 C++。 我正在尝试打印 adjacencyNodes,但它显示错误,我不明白为什么
这里是代码
列表类
#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 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;
}
我在这里遇到了错误
cout<<"\n"<<G.adjacencyNodes(x);
错误:'operator
有没有办法打印出来?
【问题讨论】:
-
错误是不言自明的。没有为您的
List类定义operator<<。你需要实现一个。 -
所有现有的流操作符实际上都是标准库提供的重载:e.g. std::stream::operator<<()。对于您的
List课程,您必须重载另一个课程:std::ostream& operator<<(std::ostream&, const List&)。仅供参考:Overloading input/output operators in C++
标签: c++ class graph compiler-errors cout