简体   繁体   中英

error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'List')

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?

You will need to overload << operator in order to use it. As you said you are learning C++ newly, so I would suggest to add a method show() to List Class to show its contents.

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;
    }
};

And then you just need a call to that Method like

G.adjacencyNodes(x).show();

And you are done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM