user3040019
user3040019

Reputation: 61

how to access a private vector of linked lists

[A,0]->[B,3]
[B,0]->[A,3]

This is the data structure i plan to use where the Y coord is the vector and the x coord is the list. each node in the list will contain a string and a integer (as shown above). lets pretend this is the class that contains the declaration of the vector of linked lists, well call it Graph, since this is a graphing assignment...[Note this code wont compile as i sketched it up to make it look simpler for others to read.]

class Graph
{
public:
    Graph(){...}
    ~Graph(){...}
private:
    class Edge
    {
    public:
        Edge(string vertex, int weight)
        {
            m_vertex = vertex;
            m_weight = weight;
        }
        ~Edge(){...}
        string m_vertex;
        int m_weight;
    };
    vector< list < Edge > > adjacency_list; //the vector of linked lists
};

In a completely different .h file I would have this class declared:

class Modify_Graph
{
public:
    void access_Edge();
    //......
private:
    //......
};

this is contained in the Modify_graph.cpp file

void Modify_Graph::access_Edge()
{
    adjacency_list adjList;
    cout << "The very first vertex is: ";
    cout << adjList[0].front().m_vertex << endl;
}

when I compile that it tells me that it cannot find 'adjacency_list' is there a way I could get it? In a more complex program I tried passing it by reference, returning it, and other things but none of them seemed to work. I am completely unsure what to do.

Upvotes: 0

Views: 105

Answers (4)

Xephon
Xephon

Reputation: 393

adjacency_list, as declared as private member, could not be seen out of the instance/object scope. This means when calling access_edge, the function would not have the permit to even be aware of the data structure.

You have to keep clear mind that OOP is all about data ownership and access right. This is what keywords like private, public, etc are all about. To achieve what you want to do, you can use one of the following approaches:

  1. Declare adjacency_list as public member by add public: before the line.

  2. If you really want to hide the data structure, create public access functions like access_adjacency_list and modify_adjacency_list that allow accessing the data. If you need to pass out a list, pass a copy of the list.

And yes, you need to pass the object instance around for access as you declared the list to be none static.

Upvotes: 0

splrs
splrs

Reputation: 2432

You need an instance of Graph from which to access adjacency_list, as well as (given that it's private) a method that for accessing the member e.g. something like

vector< list < Edge > > Graph::GetAdjacencyList()
{
    return adjacency_list;
}

Additionally, you also need to make Edge at least public in Graph or declare it outside of Graph.

If you kept it as a public inner class, your function prototype would be

vector< list < Graph::Edge > > Graph::GetAdjacencyList().

Use of the function would then be something to the effect of

void Modify_Graph::access_Edge()
{
    vector< list < Graph::Edge > > adjList = m_graph.GetAdjacencyList(); //m_graph being a member of type Graph
    cout << "The very first vertex is: ";
    cout << adjList[0].front().m_vertex << endl;
}

Upvotes: 2

Raydel Miranda
Raydel Miranda

Reputation: 14360

Why don't you try to follow some OOP principles (Head First: Object Oriented Analysis and Design) and make Edge class declaration out of Graph declaration?

class Edge
{
public:
    Edge(string vertex, int weight)
    {
        m_vertex = vertex;
        m_weight = weight;
    }
    ~Edge(){...}
    string m_vertex;
    int m_weight;
};

class Graph
{
public:
    Graph(){...}
    ~Graph(){...}
private:
    vector< list < Edge > > adjacency_list; //the vector of linked lists
};

Then you can pass an edge to the function that access an edge, and only the edge you want modify not the entire list. Seriously you have some design issues in your code, read the book I recommended, and your live will be easier.

This way you can even reuse the Edge class for others projects.

Upvotes: 0

Manuel
Manuel

Reputation: 21

In oop, it is not possible to access non static fields in a static way. You should deliver the class Modify_Graph, reference to the Graph instance and implement a getter or something for the adjacency_List.

Upvotes: 1

Related Questions