Image

Imagederralf wrote in Imagecpp

Hello. I'm new to c++ and I am trying to program a graph class template with unspecified vertex and edge objects. My problem is, how do I reference a type which is defined inside the template class outside of its definition? The problem is illustrated in the following piece of code.

template <class TV, class TE> class graph { public: class vertex; // forward declaration of a vertex typedef vertex * VPtr; // a pointer to a vertex typedef map<VPtr,TE> Eset; // a set of edges; // VPtr points to a target vertex // and TE is some edge data (e.g capacity) class vertex : public TV // endows a custom vertex of type TV // with a set of neighbors nn { Eset nn; }; typedef list<vertex> Vlist; // a list of vertices VPtr choose_random_vertex(); // chooses vertex randomly private : Vlist V; }; template <class TV, class TE> VPtr graph<TV,TE>::choose_random_vertex() // error : expected constructor, destructor, // or type conversion before 'graph' { // choose vertex randomly }


Obviously the implementation of the function choose_random_vertex() does not compile because VPtr is not known. It would work if I gave an inline definition of the function inside the graph template, but this I don't want.

It is most definitely an issue of programming style. Which is what I have no experience with in c++. Hints and nudges are highly appreciated.