mercredi 27 avril 2016

Creating a graph data structure in C++ error

I am trying to create a graph data structure and I have a couple questions. First, why am I getting this error?

In file included from main.cpp:2:0:
graph.h: In instantiation of ‘void graph<T>::addVertex(const T&) [with T = int]’:
main.cpp:8:17:   required from here
graph.h:72:5: error: ‘int graph<int>::vertex::data’ is private
   T data;   //vertex stores data of any type
     ^
graph.h:103:52: error: within this context
     std::cout << "Just created vertex with data: " << x.data << std::endl;
                                                    ^
graph.h: In instantiation of ‘graph<T>::vertex::vertex(const T&) [with T = int]’:
graph.h:102:18:   required from ‘void graph<T>::addVertex(const T&) [with T = int]’
main.cpp:8:17:   required from here
graph.h:72:5: warning: ‘graph<int>::vertex::data’ will be initialized after [-Wreorder]
   T data;   //vertex stores data of any type
     ^
graph.h:57:8: warning:   ‘bool graph<int>::vertex::visited’ [-Wreorder]
   bool visited;  //used for paths. True if vertex has been visited
        ^
graph.h:60:6: warning:   when initialized here [-Wreorder]
      vertex(const T& d = T{}): data(d), visited(false){}

Basically I am creating a graph class and declaring/ using a vertex and edge class in it. Here is the code for that:

      class vertex{
           public:
                bool visited;           //used for paths. True if vertex has been visited

                //vertex constructor
                vertex(const T& d = T{}): data(d), visited(false){}
                //vertex move constructor
                vertex(T&& d): data(std::move(d)), visited(false){}
                //returns data in vertex
/*              T& operator*(){ return retrieve(); }
                //returns const reference to data in vertex
                const T& operator*() const{ return retrieve(); }
                //returns list of adjacent vertices
                std::list<vertex>& getList() const{ return adjacent; }*/
                //adds vertex to current vertex's adjacency list
                void addToList(const vertex& add){ adjacent.push_back(add); }
           private:
                T data;                 //vertex stores data of any type
                std::list<vertex> adjacent;     //list of adjacent vertices

                T& retrieve() const{ return data; }
        };

This is declared within class Graph{};

What is the error about? And what is the best way to implement from scratch, I would like to work on some cool projects with it.

***EDIT: IN CASE IT HELPS HERE IS THE FULL CLASS

#ifndef GRAPH_H_
#define GRAPH_H_

#include <iostream>
#include <list>
#include <vector>
#include <string>
//#include "edge.h"

template <typename T>
class graph{

   private:
        class vertex;           //FORWARD DECLARATION OF CLASS VERTEX

        //We are declaring the edge class first so the rest of the graph class functions
        //know it exists
        class edge{
           public:
                //default edge constructor
                edge(): start(nullptr), end(nullptr), weight(0){;}
                //constructor with two vertex parameters creates edge
                edge(const vertex& x, const vertex& y, int w)
                   :start(x), end(y), weight(w) {;}
                //edge destructor
                ~edge(){
                   delete start;
                   delete end;
                };

                //edge copy constructor
                edge(const edge& e){
                   start = e.start;
                   end = e.end;
                   weight = e.weight;
                };

                //edge move copy constructor using move semantics
                edge(edge&& e){
                   start = std::move(e.start);
                   end = std::move(e.end);
                weight = e.weight;
                };

                //edge move copy constructor using move semantics
                edge(edge&& e){
                   start = std::move(e.start);
                   end = std::move(e.end);
                   weight = e.weight;
                };

//              typename std::list<vertex>::iterator findEdge(std::list<vertex>& l, vertex a, vertex b);
           private:
                vertex* start;
                vertex* end;
                int weight;

                friend class graph<T>;
        };   //END OF EDGE CLASS

        //Each vertex holds a list of adjacent vertices
        class vertex{
           public:
                bool visited;           //used for paths. True if vertex has been visited

                //vertex constructor
                vertex(const T& d = T{}): data(d), visited(false){}
                //vertex move constructor
                vertex(T&& d): data(std::move(d)), visited(false){}
                //returns data in vertex
/*              T& operator*(){ return retrieve(); }
                //returns const reference to data in vertex
                const T& operator*() const{ return retrieve(); }
                //returns list of adjacent vertices
                std::list<vertex>& getList() const{ return adjacent; }*/
                //adds vertex to current vertex's adjacency list
                void addToList(const vertex& add){ adjacent.push_back(add); }
           private:
                T data;                 //vertex stores data of any type
                std::list<vertex> adjacent;     //list of adjacent vertices

                T& retrieve() const{ return data; }
        };

   public:
        //graph default constructor
        graph();
        //graph destrcutor
        ~graph();
        //graph constructor with 2 vertex parameters
        graph(const vertex& x, const vertex& y);
        //graph copy constructor
        graph(const graph& g);
        //graph copy move constructor
        graph(graph&& g);
        //graph assignment operator
        graph& operator=(const graph& g);
        //graph move assignment operator
        graph& operator=(graph&& g);
/*      //graph constructor with linked list of edges parameter
        graph(const list<edge>& edges);
*/

        //returns vertex
        vertex& getVertex(const T& data);
        //creates and adds a vertex to the graph
        void addVertex(const T& data){
           vertex x(data);
           std::cout << "Just created vertex with data: " << x.data << std::endl;
           vertices.push_back(x);
        }
        //connects an edge to the graph
        void addEdge(const vertex& x, const vertex& y);
        //determines whether first vertex is reachable by the second vertex
        bool isReachable(const vertex& start, const vertex& end) const;
        //returns list of adjacent neighbors of vertex
        std::list<vertex>& getNeighbors(const vertex& v){ return v.getList(); }

   private:
        size_t numVertices;
        size_t graphWeight;
        std::list<vertex> vertices;
        std::list<edge> edges;


};   //END OF GRAPH CLASS

   #include "graph.hpp"

Thanks

Aucun commentaire:

Enregistrer un commentaire