无法从派生类调用基类方法

时间:2017-05-04 19:57:04

标签: c++ inheritance

好。所以我有一个这样的代码:

igraph.h

#include <iostream>
#include <vector>
#include <array>

class igraph
{
protected:
    std::vector<std::vector<int>> matrix; // reprezentacja macierzowa grafu
    std::vector<std::vector<int>> adjacency_list; // reprezentacja w postaci listy sasiadow
    int number_of_vertices;
    int number_of_edges;
public:
    igraph();

    igraph(std::vector<std::vector<int>> data, int vertices); // konstruktor przyjmujacy dane w postaci wierzcholek, wierzcholek, waga/przepustowosc

    const std::vector<std::vector<int>> &getMatrix() const;

    const std::vector<std::vector<int>> &getAdjacency_list() const;

    unsigned int getNumber_of_vertices() const;

    unsigned int getNumber_of_edges() const;

    void print_matrix();

    void print_list();

};

igraph.cpp

#include "igraph.h"
#include <iomanip>

/*
 * Interfejs dla grafow - nieskierowanych i skierowanych
 */

igraph::igraph() : number_of_vertices(0), number_of_edges(0)
{

}

igraph::igraph(std::vector<std::vector<int>> data, int vertices)
{
    this->number_of_edges = data.size();
    this->number_of_vertices = vertices;
    this->matrix = std::vector<std::vector<int>>(number_of_vertices, std::vector<int>(number_of_vertices, -1)); // zainicjalizowanie macierzy wartosciami -1
    this->adjacency_list = std::vector<std::vector<int>>(number_of_vertices, std::vector<int>());
}

const std::vector<std::vector<int>> &igraph::getMatrix() const {
    return matrix;
}

void igraph::print_matrix()
{
    std::cout << '\n' << "Reprezentacja macierzowa: " << '\n';
    std::cout << "    ";
    int i = 0;
    for (i = 0; i < this->number_of_vertices; i++)
    {
        std::cout << std::setw(4) << i << " ";
    }
    std::cout << "\n";
    i = 0;
    for (auto row = this->matrix.begin(); row!=this->matrix.end(); ++row )
    {
        std::cout << std::setw(4) << i;
        for (auto col = row->begin(); col!=row->end(); ++col)
        {
            std::cout << std::setw(4) << *col << " ";
        }
        std::cout << '\n';
        i++;
    }
}

void igraph::print_list()
{
    std::cout << '\n' << "Reprezentacja w postaci listy sasiadow: " << '\n';
    int i, x;
    for (i = 0; i < this->number_of_vertices; i++)
    {
        std::cout << std::setw(4) << i << " ";
        for (x = 0; x < adjacency_list[i].size(); x++)
        {
            std::cout << std::setw(4) << adjacency_list[i][x] << " ";
        }
        std::cout << '\n';
    }

}

const std::vector<std::vector<int>> &igraph::getAdjacency_list() const
{
    return adjacency_list;
}

unsigned int igraph::getNumber_of_vertices() const
{
    return number_of_vertices;
}

unsigned int igraph::getNumber_of_edges() const
{
    return number_of_edges;
}

现在我想从igraph派生。这就是我创建一个名为directed_graph.h的类:

的原因
#pragma once
#include <vector>
#include "igraph.h"

class directed_graph: public igraph
{
public:
    directed_graph();
    directed_graph(std::vector<std::vector<int>> data, int vertices);
};

directed_graph.cpp

#include "directed_graph.h"

directed_graph::directed_graph() : igraph()
{
}

directed_graph::directed_graph(std::vector<std::vector<int>> data, int vertices) : igraph(data, vertices)
{
    int row, col, cost;
    for (int i = 0; i<number_of_edges; i++) // wpisywanie danych do macierzy
    {
        row = data[i][0];
        col = data[i][1];
        cost = data[i][2];
        matrix[row][col] = cost;
    }
    int current_search = 0;
    for (int x = 0; x < number_of_vertices; x++)
    {
        for (int i = 0; i<number_of_edges; i++)
        {
            if (data[i][0] == current_search)
            {
                adjacency_list[current_search].push_back(data[i][1]);
            }
        }
        current_search++;
    }

}

现在位于其他.cpp文件(我同时包含igraph.hdirected_graph.h)的某个地方,我directed_graph myGraph = directed_graph(data, vertices)。然后我想用print_matrix()print_list()打印出来。我得到了这些错误:

'igraph': 'class' type redefinition 'igraph': base class unindefined 'print_matrix': is not a member of directed_graph 'print_list': is not a member of directed_graph

这里有什么问题?我认为它应该有用,我在我看来是正确的,但后来出了什么问题

2 个答案:

答案 0 :(得分:1)

'igraph': 'class' type redefinition应该告诉您正在重新定义class igraph。这是因为你要包括igraph.h两次;一个来自你的&#34;其他&#34; .cpp文件,一次来自directed_graph.h。您忘记了#pragma once

答案 1 :(得分:0)

你应该使用.h的守卫来避免重新定义。

#ifndef  _X_H_
#define  _X_H_


....


#endif // _X_H_