469,904 Members | 2,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,904 developers. It's quick & easy.

problems with friend class in templates

First time posting, thanks in advance for any help you can give me.
Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i created.

But, I can't get the friend class to work. For example (this is not actually my code):

template <typename Key, typename Value> class MyMap;
template <typename Key, typename Value> struct MyMapNode;

class MyIterator {
friend class MyMap<Key, Value>;
public:
?
?
?

private:
pair<Key, Value> * pointee;
MyMapNode<Key, Value> * nodee;
MyIterator(pair<Key, Value> *pointee = NULL, MyMapNode<Key, Value> *nodee = NULL) : pointee(pointee), nodee(nodee) {}
};

template <typename Key, typename Value>
class MyMap {
public:
typedef MyIterator<Key, Value> iterator;
typedef MyIterator<Key, Value> const_iterator;
?
};

Given that, the following line doesn?t work in a test file:

#include "TheHeaderFileWithTheStuffIPutAbove.h"
...
MyMap<string, int>::iterator iter;

which is to say, i can't create any instance of iterator because the constructor is private (though it is supposed to be a friend)

i get an error about how the constructor can not be accessed because it is private.

This problem has been plaguing me for a long time. Any help would be appreciated.

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>RaZbhxmDDkyiNriA7sn3vw==</Id>
Nov 17 '05 #1
1 1280
Joe Carner via .NET 247 wrote:
First time posting, thanks in advance for any help you can give me.
Basically I am trying to a class that i want to be able to access the
private data members of another class, both of which i created.

But, I can't get the friend class to work. For example (this is not
actually my code):

template <typename Key, typename Value> class MyMap;
template <typename Key, typename Value> struct MyMapNode;

class MyIterator {
Is this class a template? Is it nested in one of the classes above?
friend class MyMap<Key, Value>;
public:
?
?
?

private:
pair<Key, Value> * pointee;
MyMapNode<Key, Value> * nodee;
MyIterator(pair<Key, Value> *pointee = NULL, MyMapNode<Key, Value>
*nodee = NULL) : pointee(pointee), nodee(nodee) {} };

template <typename Key, typename Value>
class MyMap {
public:
typedef MyIterator<Key, Value> iterator;
typedef MyIterator<Key, Value> const_iterator;
?
};

Given that, the following line doesn?t work in a test file:

#include "TheHeaderFileWithTheStuffIPutAbove.h"
..
MyMap<string, int>::iterator iter;
Where does this declaration appear - is it inside a member function of
MyMap<string,int>? If not, the friend declaration doesn't help you any.
which is to say, i can't create any instance of iterator because the
constructor is private (though it is supposed to be a friend)

i get an error about how the constructor can not be accessed because
it is private.

This problem has been plaguing me for a long time. Any help would be
appreciated.


Hopefully this sample can get you pointed in the right direction. Note that
the map class and the iterator class are friends of each other.

<code>

template <class K, class V> class MapIterator;

template <class K, class V> class MyMap
{
friend class MapIterator<K,V>;

struct MapNode
{
};

MapNode* m_node;

public:
typedef MapIterator<K,V> iterator;

iterator begin();
};

template <class K, class V> class MapIterator
{
public:
MapIterator()
{
}

private:
friend class MyMap<K,V>;
typedef typename MyMap<K,V>::MapNode NodeType;

MapIterator(NodeType* node)
: m_node(node)
{
}

NodeType* m_node;
};

template<class K, class V>
typename MyMap<K,V>::iterator MyMap<K,V>::begin()
{
return iterator(m_node);
}
typedef MyMap<int,float> MIF;

void test()
{
MIF mif;
MIF::iterator it = mif.begin();
MIF::iterator it2;
}

</code>

-cd
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Alexander Stippler | last post: by
4 posts views Thread by Bonzo | last post: by
21 posts views Thread by Sebastian Faust | last post: by
2 posts views Thread by Christophe Barbe | last post: by
1 post views Thread by Don Kim | last post: by
6 posts views Thread by Starx | last post: by
5 posts views Thread by Angel Tsankov | last post: by
9 posts views Thread by Adam Badura | last post: by
21 posts views Thread by H9XLrv5oXVNvHiUI | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.