【问题标题】:Upcasting in C++ : cannot convert child class pointer to parent class pointer在 C++ 中向上转换:无法将子类指针转换为父类指针
【发布时间】:2016-07-26 04:53:15
【问题描述】:

我的程序有问题。我用 C++ 制作了一个 Graph 类,现在我想对它进行拓扑排序。问题是,我的拓扑排序接受任何 DirectedGraph,但是当我想给它一个孩子(例如 AdjacencyListDirectedUnweightedGraph)时,它拒绝转换。这是我的 .hpps:

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

对不起那个没用的帖子!

【问题讨论】:

  • 你在哪一行得到错误?

标签: c++ casting upcasting


【解决方案1】:

强制转换为引用而不是对象本身,这很愚蠢,抱歉!

【讨论】:

    猜你喜欢
    • 2015-08-12
    • 2021-09-20
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多