472,374 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

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 1346
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
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.