I am trying to implement an Adjacent list using an array of linked lists:
vector< list<Edge> > adjList;
at the moment, I cannot get it to compile because I am not quite sure how to access the
vertex or even the weight of my nested class, Edge
. This is the error output...
Graph.cpp: In member function `void Graph::set_Edge(std::string, std::string, int)':
Graph.cpp:30: error: 'class std::list<Graph::Edge, std::allocator<Graph::Edge> >' has no member named 'm_vertex'
makefile.txt:9: recipe for target `Graph.o' failed
make: *** [Graph.o] Error 1
here is the class declarations in the file Graph.h (sorry about all the comments, I like to keep all of my thoughts left on my code until i am ready to turn it in...)
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
//class MinPriority;
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
class Graph
{
public:
Graph();
~Graph();
/*void setArray(string vertex);
void sortArray();
Graph* getGraph(string preVertex, string vertex, int weight);
void setGraph(string vertex, string postVertex, int weight);*/
void set_Edge(string targetVertex, string vertex, int weight);
friend class MinPriority;
private:
class Edge
{
public:
Edge(string vertex, int weight)
{m_vertex = vertex; m_weight = weight;}
~Edge();
string get_vertex(){}
int get_weight(){}
private:
string m_vertex;
int m_weight;
};
vector< list<Edge> > adjList;
};
#endif // GRAPH_H_INCLUDED
Here is Graph.cpp
#include "Graph.h"
void Graph::set_Edge(string targetVertex, string vertex, int weight) //find target vertex
{
for(unsigned int u = 0; u <= adjList.size(); u++)//traverses through the Y coord of the 2D array
{
if(targetVertex == adjList[u].m_vertex) // <<THIS IS WHERE THE ERROR OCCURS!!!!
{
adjList[u].push_back(Edge(vertex, weight)); //push new element to the back of the linked list at array index u.
}
}
//we now need to add
//adjList[adjList.size()].push_back(Edge(vertex, weight));
}
Again, I basically need help finding out how to access parts of Edge (either vertex or weight) if(targetVertex == adjList[u].m_vertex)
is what I hoped to use thinking it would find the 'u' index in the array and check the 'u' index's vertex element and compare it to target vertex.
adjList is of type
list<Edge>
, so it doesn't have any members namedm_vertex
. It's the nodes it contains that havem_vertex
members.