Ich habe ein Problem mit meinem Programm. Ich habe eine Graph-Klasse in C++ erstellt, und jetzt möchte ich sie topologisch sortieren. Die Sache ist, meine topologische Art akzeptiert jeden DirectedGraph, aber wenn ich ihr ein Kind geben möchte (AdjacencyListDirectedUnweightedGraph zum Beispiel), weigert sie sich zu konvertieren. Hier ist meine .hpps:Upcasting in C++: Kind Klassenzeiger kann nicht in Elternklassenzeiger umgewandelt werden
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
Und, mein Haupt.
#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);
}
Das war's! Es tut mir leid, wenn es offensichtlich oder so aussieht, ich kann einfach nicht sehen, was das Problem ist. Auch versuchte dynamische und statische Würfe, kein Erfolg. Danke im Voraus !
EDIT:
Ich war dumm. versucht, für eine Referenz, statt ein Objekt selbst ...
static_cast statt static_cast
Sorry für die nutzlos Post zu werfen!
Auf welcher Zeile erhalten Sie den Fehler? –