0

Given example DirectedGraph:

What is the definition of the two nested class' constructors? Based on my observation below, I am guessing it has to do with the constructor's parameters being a sibling class.

DirectedGraph.tpp

template < class Key,
           class Compare   = std::less<Key>,
           class Allocator = std::allocator<Key> >
class DirectedGraph
{
public:
    // only used for returns/queries
    class Edge
    {
    public:
        Edge(Vertex&, 
             Vertex&);

    private:
        Vertex& from;
        Vertex& to;
    };


    class Vertex
    {
    public:
        Vertex(Key&);
        ~Vertex(void);

    private:
        value T&;
        std::set<Vertex *> edges;
    };

    DirectedGraph(void);
    ~DirectedGraph(void);

    <...lots of methods...>

private:
    size_t num_edges;
    std::set<DirectedGraph<Key, Compare, Allocator>::Vertex *> vertices;
};

Visual Studio give me an error (E0147 declaration is incompatible with "DirectedGraph<Key, Compare, Allocator>::Edge::Edge(<error-type> &, <error-type> &)" )

DirectedGraph.cpp

template <class Key, class Compare, class Allocator>
DirectedGraph<Key, Compare, Allocator>::Edge::Edge
(
    Vertex& from,
    Vertex& to
)
{

}

but curiously, not with the Vertex constructor:

template<class Key, class Compare, class Allocator>
DirectedGraph<Key, Compare, Allocator>::Vertex::Vertex
(
    Key& value
)
{

}

I am guessing this has to do with the latter not requiring its sibling nested class as a constructor parameter?

Thanks in advance, I am completely stumped.

Edit #1

Forward declaring (or changing position) as per quimby's comment or user7860670's comment did the trick for the errors. With a few more tweaks, went from 168 errors to 1.

DirectedGraph.tpp

template < class Key,
           class Compare   = std::less<Key>,
           class Allocator = std::allocator<Key> >
class DirectedGraph
{
public:
    // only used for returns/queries
    class Edge;
    class Vertex;

    class Edge
    {
    ... everything else identical to above
}

DirectedGraph.cpp

template<class Key, class Compare, class Allocator>
DirectedGraph<Key, Compare, Allocator>::Vertex::Vertex
(
    Key& value
)
{

}

template <class Key, class Compare, class Allocator>
DirectedGraph<Key, Compare, Allocator>::Edge::Edge
(
    Vertex& from,
    Vertex& to
)
{

}
SKNB
  • 75
  • 2
  • 9
  • 3
    Try moving `Vertex` class definition above `Edge`. If you want help, post a [mcve] - this includes the errors you are getting. – Quimby Jun 05 '23 at 17:33
  • 2
    You should add a forward declaration prior to `Edge` definition `class Vertex;` Or just rearrange `Edge` and `Vertex`. – user7860670 Jun 05 '23 at 17:38
  • 1
    ***Visual Studio give me an error with*** What error? At StackOverflow you need to always show the text of the error message as text. For this code I expect linker errors unless you use explicit intimidations because you implemented your templates in a cpp file. Related: [https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – drescherjm Jun 05 '23 at 17:42
  • 1
    ***explicit intimidations*** Oops, looks like auto correct got me.. Should have been explicit instantiations. – drescherjm Jun 05 '23 at 19:11
  • @Quimby worked like a charm! thank you so much. it seems so obvious now! but i tried every possible permutation of DirectedGraph<>, typename, etc. – SKNB Jun 05 '23 at 19:36
  • @user7860670 yours would have worked as well, thank you so much – SKNB Jun 05 '23 at 19:37
  • @drescherjm edited post to include this – SKNB Jun 05 '23 at 19:45
  • Down from 168 errors to 1! One last question: for the DirectedGraph constructor... how would one initialize a STL `set` of type `Vertex *`? `this->vertices = new std::set();` gets me: "`C2679` _binary_ '=': _no operator found which takes a right-hand operand of type_ `'std::set,std::allocator>::Vertex *,std::less,std::allocator>::Vertex *>,std::allocator,std::allocator>::Vertex *>> *'` (or there is no acceptable conversion)" – SKNB Jun 05 '23 at 19:50

0 Answers0