0
[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.

user3040019
  • 51
  • 2
  • 10
  • 1
    `adjacency_list` is a member object of `Graph`, but in `access_Edge` you're trying to use it like a type. – Joseph Mansfield Dec 20 '13 at 13:19
  • How could I access the member object and use it as an object in a separate file? Also I forgot to note that Modify_graph.cpp has #includes for both of the .h files. – user3040019 Dec 20 '13 at 13:21
  • @user3040019 It is not advisable to directly access the object (unless the class which contains it is meant to be container-like) as that breaches encapsulation. Add appropriate methods to `Graph` which provide the necessary access, and use those methods instead of accessing the object directly. – JBentley Dec 20 '13 at 13:24
  • Let's just call them "lists". The implementation mechanic is abstracted away from you. – Lightness Races in Orbit Dec 20 '13 at 14:28

4 Answers4

2

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;
}
splrs
  • 2,424
  • 2
  • 19
  • 29
  • Yeah, I tried doing that many times but it would always spit out: Graph.h:26: error: template argument 1 is invalid. – user3040019 Dec 20 '13 at 13:33
  • 1
    Don't be insulted by the question, but did you check the brackets matched when declaring/defining the function? http://stackoverflow.com/questions/12323929/c-class-template-is-a-template-template-argument-is-invalid – splrs Dec 20 '13 at 13:37
  • Yes, and this is what I wrote in my main program: vector < list < Edge > > get_adjList(){return adjList;} – user3040019 Dec 20 '13 at 13:39
  • Graph.h:26: error: `Edge' was not declared in this scope Graph.h:26: error: template argument 1 is invalid Graph.h:26: error: template argument 2 is invalid Graph.h:26: error: template argument 1 is invalid Graph.h:26: error: template argument 2 is invalid Graph.h:26: error: ISO C++ forbids declaration of `Get_Adj_list' with no type Graph.h: In member function `int Graph::Get_Adj_list()': Graph.h:26: error: cannot convert `std::vector >, std::allocator > > >' to `int' in return – user3040019 Dec 20 '13 at 13:40
  • 1
    What's `adjList`? Looks like it's an int? – splrs Dec 20 '13 at 13:45
  • adjList is the same as adjacency_list (I made the code above by scratch so it would be easier to explain.) in the real program it is declared as vector< list > adjList; – user3040019 Dec 20 '13 at 13:50
  • I flipped the contents of graph so it goes from private {...} and then public {...} that way my getter function can see the Edge class. Not sure if thats going to fix all my problems but I am making progress! – user3040019 Dec 20 '13 at 13:59
  • 1
    In the original question I'm seeing Edge as being in the private section, have you changed that? You need other classes to able to see `Edge` (unless you used friend classes). I've made additions to my answer so check they're all addressed. – splrs Dec 20 '13 at 14:00
1

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.

Leandros
  • 16,805
  • 9
  • 69
  • 108
Manuel
  • 21
  • 5
  • Yes! I tried implementing that in my program but I could NEVER get it to work! In the program all i need is to deliver 1 linked list at a time. I am unsure what the code is to pass a vector of linked lists by reference. That is what I had been trying to do for the past 12 hours. – user3040019 Dec 20 '13 at 13:29
0

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.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
0

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.

Xephon
  • 393
  • 1
  • 12