我有这两个类,我希望边缘节点成为图形的朋友,但我不知道如何声明它
图表的实现基于邻接列表,我使用unique_ptr进行自动删除。
#ifndef GRAPH_HPP
#define GRAPH_HPP
#include <memory>
using std::unique_ptr;
template<typename Type>
class edgenode {
friend class graph<Type>; //here
public:
edgenode(Type w, int adj);
~edgenode() {};
private:
Type mWeight;
int mAdj;
unique_ptr<edgenode<Type>> mNext;
};
template<typename Type>
edgenode<Type>::edgenode(Type w, int adj)
:mWeight(Type), mAdj(adj) {
mNext = nullptr;
}
///-------------------------------------------------///
template<typename Type>
class graph {
public:
graph(int maxvertices = 1000, bool directed = false);
~graph();
private:
unique_ptr<edgenode<Type>>* mEdges;
int* mDegree;
int mNoOfNodes;
int mNoOfVertices;
int mSize;
bool m_bDirected;
};
template<typename Type>
graph<Type>::graph(int maxvertices = 1000, bool directed = false)
: mSize(maxvertices),
m_bDirected(directed),
mNoOfNodes(0),
mNoOfVertices(0)
{
mDegree = new int[mSize];
mEdges = new unique_ptr<edgenode<Type>>[mSize];
for (int i = 0; i < mSize; ++i) {
mDegree[i] = 0;
mEdges[i] = nullptr;
}
}
template<typename Type>
graph<Type>::~graph() {
delete[] mEdges;
delete[] mDegree;
}
#endif
答案 0 :(得分:1)
转发声明类模板,如下所示:
override bindIXtextBuilderParticipant() {
MyBuilderParticipant
}
答案 1 :(得分:0)
在edgenode声明之前添加template class graph<class T>;
,它被称为前向声明:
#include <memory>
using std::unique_ptr;
template class graph<class T>; // this
template<typename Type>
class edgenode {
friend class graph<Type>; //here
public:
...