Нужна помощь, чтобы узнать, какой синтаксис работает с моей структурой данных

Я пытаюсь реализовать смежный список, используя массив связанных списков:

vector< list<Edge> > adjList;

на данный момент я не могу заставить его скомпилировать, потому что я не совсем уверен, как получить доступ к

вершина или даже вес моего вложенного класса, Edge, Это ошибка вывода …

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

вот объявления классов в файле Graph.h (извините за все комментарии, я хотел бы оставить все свои мысли в своем коде, пока я не буду готов включить его …)

    #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

Вот это 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));
}

Опять же, мне в основном нужна помощь, чтобы узнать, как получить доступ к частям Edge (вершина или вес) if(targetVertex == adjList[u].m_vertex) это то, что я надеялся использовать, думая, что он найдет индекс ‘u’ в массиве, проверит элемент вершины индекса ‘u’ и сравнит его с целевой вершиной.

0

Решение

adjList имеет тип list<Edge>так что у него нет имен m_vertex, Это узлы, которые он содержит, имеют m_vertex члены.

#include "Graph.h"
void Graph::set_Edge(string targetVertex, string vertex, int weight)
{
for(unsigned int u = 0; u <= adjList.size(); u++)
{
if(adjList[u].front().m_vertex == targetVertex)
//^^add front() to access a list node
//because the m_vertex member is same of all the edges in the list
//So just access the first one for simplicity.
{
adjList[u].push_back(Edge(vertex, weight));
}
}
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector