-2

Okay. So I've got a code like this:

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

Now I want to derive from igraph. That's why I create a class called 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++;
    }

}

Now somewhere in other .cpp file (where I include both igraph.h and directed_graph.h) I do directed_graph myGraph = directed_graph(data, vertices). Then I want to print it out with print_matrix() and print_list(). I get those errors:

'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

What is wrong here? I thought it should work, I derive correctly in my opinion, but then what is wrong

minecraftplayer1234
  • 2,127
  • 4
  • 27
  • 57

2 Answers2

1

'igraph': 'class' type redefinition should be telling you that you are redefining class igraph. That's because you are including igraph.h twice; one from your "other" .cpp file and once from directed_graph.h. You have forgotten a #pragma once.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Can I actually `#pragma once` in a `.cpp` file? I moved the `#include` to `.h` file, cuz the problematic one is `menu.cpp` and it includes `menu.h`, where I do have `#pragma once`. But that did the job, thanks – minecraftplayer1234 May 04 '17 at 20:03
  • no, you have forgotten to put `#pragma once` in `igraph.h` – Mike Nakis May 04 '17 at 20:04
0

You should use guard for .h so that redefinition avoided.

#ifndef  _X_H_
#define  _X_H_


....


#endif // _X_H_
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65