【问题标题】:error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List')错误:'operator<<' 不匹配(操作数类型是 'std::ostream {aka std::basic_ostream<char>}' 和 'List')
【发布时间】: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&lt;&lt;。你需要实现一个。
  • 所有现有的流操作符实际上都是标准库提供的重载:e.g. std::stream::operator<<()。对于您的List 课程,您必须重载另一个课程:std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const List&amp;)。仅供参考:Overloading input/output operators in C++

标签: c++ class graph compiler-errors cout


【解决方案1】:

您需要重载

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();

你就完成了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2016-12-01
    相关资源
    最近更新 更多