gpt4 book ai didi

c++ - 错误: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'List' )

转载 作者:行者123 更新时间:2023-12-02 10:56:23 25 4
gpt4 key购买 nike

最近,我正在学习C++。
我正在尝试打印adjacencyNodes,但显示错误,我不明白为什么
这是代码
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 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 <<”不匹配(操作数类型为“std::ostream {aka std::basic_ostream}”和“List”)
有什么办法可以打印出来吗?

最佳答案

您将需要重载<<运算符才能使用它。如您所说,您是新学习C++,所以我建议向List Class添加一个方法show()以显示其内容。

class List
{
int list[100];
int size = 0;

public:
void add(int a)
{
list[size] = a;
size++;
}

int get(int i)
{
return list[i];
}
void show()
{
for(int i=0;i<size;i++)
cout<<list[i];
cout<<endl;
}
};
然后,您只需要调用该方法,例如
G.adjacencyNodes(x).show();
您完成了!

关于c++ - 错误: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62909468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com