oren revenge - 1 year ago 93

C++ Question

I have those two classes and I want the edge node to be friend for graph but I don't know how to declare it

The implementation of the graph is based on adjacency lists, I used unique_ptr for the auto deletion.

`#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

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Forward declare the class template like this:

```
template<typename Type> class graph;
template<typename Type>
class edgenode {
friend graph<Type>;
// ...
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**