简体   繁体   中英

can we pass object of a class in different class constructor?

I am a beginner in C++ and badly stuck at creating a constructor of a class that uses the object of another class. while doing so I am getting the error

No matching function for call to....

Priority_que::Priority_que(graph g) {
    for (unsigned i=0; i<g.size(); i++) {
        for (unsigned j=i; j<g.size(); j++) {
            g1[i][j] = g.getnode(i)(j) ; }
    }

here Priority_que is a class that uses the graph class object in its constructor. please treat me as innocent if I am talking nonsense.

EDIT : I am adding the class Priority_que and its constructor, hope this will help.

class Prio_que: public graph {
    public:
        Prio_que(graph);
        pair<int,int> minValue() ;  
        priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> arranged ; 
        void dijkstra() ;
        int size() ;
    private:
        vector<vector<int>> g1 ;
        vector<int> parent ;
        vector<int> dist ;
        vector<bool> processed ;
        vector<pair<int,int>> dist_pair ;
};

Prio_que::Prio_que(graph g) {
    for (int i=0; i<g.size(); i++) {
        for (int j=i; j<g.size(); j++) {
            g1[i][j] = g.getnode(i,j) ; }
    }       
    parent(g.size(),-1);    //Stores Shortest Path Structure
    dist(g.size(),MAX); //Keeps shortest path values to each vertex from source
    processed(g.size(),false) ; //TRUE->Vertex is processed
    dist_pair(g.size(), make_pair(MAX, 0)) ; }

now the error is showing from parent(g.size(),-1); to the last line.

No matching function for call to....

Those are member variables and need to be initialized in the initializer list - you're trying to call them as functions, which is the reason for the error message.
You should also make the parameter a const reference to avoid copying it.

Prio_que::Prio_que(const graph& g)
    : parent(g.size(),-1),
      dist(g.size(),MAX),
      processed(g.size(),false),
      dist_pair(g.size(), make_pair(MAX, 0))
{
    for (int i=0; i<g.size(); i++) 
    {
        for (int j=i; j<g.size(); j++) 
        {
            g1[i][j] = g.getnode(i,j) ; 
        }
    }       
}

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