Friend function defined in class template is not in scope

Discussion in 'C++' started by Mike, Jan 16, 2011.

  1. Mike

    Mike Guest

    I ran into a problem when trying to write a template class to represent
    graphs.
    I define function add_edge in the body of template class graph as a
    friend of the class.
    Unfortunately I am getting "'add_edge' was not declared in this scope"
    error message at compile time.
    See code below. I am using gcc version 4.2.1.
    Any idea why it happens and how to fix it?

    Thanks,

    Mike

    #include <list>
    using namespace std;

    template<typename VERTEX /*Vertex Satellite data*/, typename EDGE=string
    /*Edge Satellite data*/>
    struct graph
    {

    struct EDGE_LIST
    {
    list< pair<EDGE, typename list<pair<VERTEX, EDGE_LIST>
    >::iterator> > edges;

    };

    typedef list< pair<EDGE, typename list<pair<VERTEX, EDGE_LIST>
    >::iterator> > edge_list;

    typedef typename edge_list::iterator edge_pointer;

    typedef list<pair<VERTEX, EDGE_LIST> > vertex_list;

    typedef typename vertex_list::iterator vertex_pointer;

    vertex_list V;

    friend edge_list& edges(vertex_pointer p){return p->second.edges;}

    vertex_pointer add_vertex(VERTEX v)
    {
    V.push_back(make_pair(v, EDGE_LIST()));
    vertex_pointer p = V.end();
    return --p;
    }

    friend edge_pointer add_edge(vertex_pointer from, vertex_pointer
    to, const EDGE& e=EDGE())
    {
    edges(from).push_back(make_pair(e,to));
    edge_pointer p = edges(from).end();
    return --p;
    }

    };


    int main()
    {
    graph<int> G;
    graph<int>::vertex_pointer a = G.add_vertex(1);
    graph<int>::vertex_pointer b = G.add_vertex(2);
    add_edge(a,b); //Compiler complains about this call
    return 0;
    }
     
    Mike, Jan 16, 2011
    #1
    1. Advertising

  2. Mike <> wrote:
    > struct graph
    > {
    > friend edge_list& edges(vertex_pointer p){return p->second.edges;}
    >
    > vertex_pointer add_vertex(VERTEX v)
    > {
    > V.push_back(make_pair(v, EDGE_LIST()));
    > vertex_pointer p = V.end();
    > return --p;
    > }
    >
    > friend edge_pointer add_edge(vertex_pointer from, vertex_pointer
    > to, const EDGE& e=EDGE())
    > {
    > edges(from).push_back(make_pair(e,to));
    > edge_pointer p = edges(from).end();
    > return --p;
    > }
    > };


    You have only declared those functions as friends, but you have not
    declared the functions themselves in the outer scope. You need to add
    those declarations to the global scope. In other words:

    edge_list& edges(vertex_pointer p);
    edge_pointer add_edge(vertex_pointer from, vertex_pointer to,
    const EDGE& e=EDGE());
     
    Juha Nieminen, Jan 16, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yueh-Wei Hu
    Replies:
    0
    Views:
    464
    Yueh-Wei Hu
    May 23, 2004
  2. Replies:
    2
    Views:
    496
    John Harrison
    Nov 9, 2005
  3. A L
    Replies:
    1
    Views:
    524
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  4. Peter
    Replies:
    8
    Views:
    2,250
    James Kanze
    Dec 20, 2010
  5. Mike
    Replies:
    6
    Views:
    784
Loading...

Share This Page