2016-04-06 4 views
0

私のプログラムに問題があります。私はC++でGraphクラスを作ったので、これをトポロジー的にソートしたいと思っています。つまり、私のトポロジカルソートは任意のDirectedGraphを受け入れますが、私はそれを子(AdjacencyListDirectedUnweightedGraphなど)にしたい場合、それは変換を拒否します。ここに私の.hppsです:C++でのアップキャスト:子クラスポインタを親クラスポインタに変換できません

TopoSort.hpp:

#ifndef TOPOSORT_HPP 
#define TOPOSORT_HPP 

#include "../Graph.hpp" 
#include "../DirectedGraph/AdjListUWDG.hpp" 
#include "../DirectedGraph/DirectedGraph.hpp" 
#include "../UnDirectedGraph/AdjListWUDG.hpp" 

class TopoSort 
{ 
protected: 
    std::vector<int> _sortedList; 
    std::vector<int> _KahnTopNodes; 
public: 
    TopoSort(); 
    ~TopoSort(); 
    void KahnSort(DirectedGraph &list); 
    void KahnSortTopNodes(DirectedGraph &list); 
}; 

#endif 

DirectedGraph.hpp

#ifndef DIRECTEDGRAPH_HPP 
#define DIRECTEDGRAPH_HPP 

#include <iostream> 
#include <string> 
#include <vector> 
#include "../Graph.hpp" 

class DirectedGraph 
: public Graph 
{ 
protected: 
    std::vector<int> _inDegree; 
    std::vector<int> _outDegree; 
public: 
    DirectedGraph(){}; 
    virtual ~DirectedGraph(){}; 
    int  inDegree(int a){return (_inDegree[a]);} 
    int  outDegree(int a){return (_outDegree[a]);} 
    bool rangeCheck(int a, int b) 
    { 
     if (a >= _vertices || b >= _vertices || a == b) 
     { 
      std::cout << "The edge " << a << " - " << b << " is invalid." << std::endl; 
      return (false); 
     } 
     return (true); 

    } 
}; 

#endif 

AdjListDG.hpp

#ifndef ADJListDG_HPP 
#define ADJListDG_HPP 

#include <string> 
#include <vector> 
#include "DirectedGraph.hpp" 

class AdjListDG 
: public virtual DirectedGraph 
{ 
protected: 
    std::vector<std::vector<std::pair<int, int> > > _adjList; 
public: 
    virtual ~AdjListDG(); 
    bool existsEdge(Edge); 
    bool existsEdge(int, int); 
    void putEdge(Edge); 
    void removeEdge(Edge); 
    int  adjacentVertices(int); 
    bool areAdjacent(int, int); 
}; 

#endif 

AdjListUWDG.hpp

#ifndef AdjListUWDG_HPP 
#define AdjListUWDG_HPP 

#include <string> 
#include <vector> 
#include "AdjListDG.hpp" 

class AdjListUWDG 
: public virtual AdjListDG 
{ 
public: 
    AdjListUWDG(std::string); 
    virtual ~AdjListUWDG(); 
}; 

#endif 

そして、私のメイン。

#include <iostream> 
#include <string> 
#include <fstream> 
#include "UnDirectedGraph/AdjListWUDG.hpp" 
#include "UnDirectedGraph/AdjListUWUDG.hpp" 
#include "UnDirectedGraph/AdjMatWUDG.hpp" 
#include "UnDirectedGraph/AdjMatUWUDG.hpp" 
#include "Assgn3/TopoSort.hpp" 


int main(int argc, char** argv) 
{ 
    if (argc != 2) 
    { 
     std::cout << "Usage : ./graph FILENAME" << std::endl; 
     return(0); 
    } 

    std::string filename = argv[1]; 
    AdjListWUDG gr(filename); 
    TopoSort tsort; 
    std::ofstream fichier("results.txt", std::ios::out | std::ios::trunc); 
    if(fichier) 
    { 
     if (gr.existsEdge(1, 2)) 
      fichier << "1 - 2 exist" << std::endl; 
     fichier << "numedge == " << gr.numEdges() << std::endl; 
     fichier << "adjver 1 == " << gr.adjacentVertices(1) << std::endl; 
     fichier << "adj 1 2 == " << gr.areAdjacent(1,2) << std::endl; 
     fichier << "adj 1 0 == " << gr.areAdjacent(1,0) << std::endl; 
     fichier << "adj 0 2 == " << gr.areAdjacent(0,2) << std::endl; 
    }  
    DirectedGraph * gr2 = &gr; 
    tsort.KahnSort(*gr2); 
} 

これだけです!私はそれが明白か何かに見える場合は申し訳ありません、私はちょうど問題が何かを見ることができません。また、動的で静的なキャストを試みましたが、成功しませんでした。前もって感謝します !

編集:

私は愚かでした。 ...代わりにオブジェクト自体を、参考のために

はstatic_castの代わりに、その役に立たないポストのため申し訳ありません

はstatic_castをキャストしてみました!

+0

エラーが発生していますか? –

答えて

0

オブジェクト自体の代わりに参照にキャストすると、それは愚かでした、申し訳ありません!

関連する問題