Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Friend function defined in class template is not in scope

Reply
Thread Tools

Friend function defined in class template is not in scope

 
 
Mike
Guest
Posts: n/a
 
      01-16-2011
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;
}

 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      01-16-2011
Mike <(E-Mail Removed)> 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());
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Friend function defined in class template is not in scope Mike C++ 6 01-20-2011 05:17 AM
Scope of a friend function defined inside a class definition + a doubt Peter C++ 8 12-20-2010 09:15 AM
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
Problem with defining template friend function of a template class. PengYu.UT@gmail.com C++ 2 11-09-2005 08:27 PM
using friend function template in class template Yueh-Wei Hu C++ 0 05-23-2004 11:36 AM



Advertisments