473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1407
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander Stippler | last post by:
Hi what's wrong about the following code? It does not compile. template <typename T> class A { public: class B; };
4
by: Bonzo | last post by:
I'm trying to write a class for a smart pointer. I'm having a problem though when trying to implement operator== with both sides being a Smart. (Or any function that I try to bring U into.) The...
21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
1
by: Don Kim | last post by:
I'm trying to compile the following code from "C++ Templates" Book by Josuttis and Vandervoorde, and I keep getting link errors: //coord.hpp #include <cstdlib> class Coord { private: int x,...
6
by: Starx | last post by:
With the following class: template <class numericType> class testClass { public: testClass(numericType val = 0); friend testClass operator +(testClass a, testClass b); private: numericType...
5
by: Angel Tsankov | last post by:
Let's say we have a class template: template< typename t > class c; and a function template: template< typename t1, typename t2 > bool operator == (
9
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized...
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.