提问人:SKNB 提问时间:6/6/2023 最后编辑:SKNB 更新时间:6/6/2023 访问量:68
在 c++ 中,如何定义模板类的嵌套类构造函数,该构造函数将同级类作为参数?
In c++, how to define a template class' nested class constructor, that takes a sibling class for parameters?
问:
给出的例子:DirectedGraph
两个嵌套类的构造函数的定义是什么?根据我在下面的观察,我猜这与构造函数的参数是兄弟类有关。
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 给我一个错误(声明与”E0147
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
)
{
}
但奇怪的是,不是使用 Vertex 构造函数:
template<class Key, class Compare, class Allocator>
DirectedGraph<Key, Compare, Allocator>::Vertex::Vertex
(
Key& value
)
{
}
我猜这与后者不需要其兄弟嵌套类作为构造函数参数有关?
提前致谢,我完全被难住了。
编辑 #1
根据 quimby 的评论或 user7860670 的评论转发声明(或更改位置)解决了错误。经过一些调整,从 168 个错误变成了 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
)
{
}
答: 暂无答案
上一个:如何在嵌套类中实例化结构?
下一个:在 c++ 中调用嵌套的静态成员
评论
Vertex
Edge
Edge
class Vertex;
Edge
Vertex