2016-04-06 25 views
0

Programımla ilgili bir sorunum var. C++ 'da bir Graph sınıfı yaptım ve şimdi topolojik olarak sıralamak istiyorum. Mesele şu ki, topolojik sıralama herhangi bir DirectedGraph'ı kabul eder, ancak bir çocuğa vermek istediğimde (örneğin AdjacencyListDirectedUnweightedGraph), dönüştürmeyi reddeder.C++ 'da Güncelleştirme: alt sınıf işaretçisini ana sınıf işaretçisine dönüştüremiyor

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 
İşte benim .hpps olduğunu 10

Ve benim ana.

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

That's it! Açıkça göründüğü için üzgünüm, sorunun ne olduğunu göremiyorum. Ayrıca dinamik ve statik dökümler denedi, başarı yok. şimdiden teşekkürler !

DÜZENLEME:

aptal oldu. o yararsız yazı için

static_cast Maalesef ait

static_cast yerine ... yerine bir nesnenin kendisi, bir başvuru için döküm çalıştı!

+0

Hata hakkında hangi satırı alıyorsunuz? –

cevap

0

Nesnenin kendisi yerine bir referansa yayın yapmak, bu aptaldı, üzgünüm!

İlgili konular