Help with friend classes

I'm trying to make a friend class, but it doesn't seem to be working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Tester;

class Test
{
public:
	typedef Tester test;
	Test(){};
	~Test(){};
};

class Tester
{
private:
	friend class test;
	Tester(){};
	~Tester(){};
};

// ...

int main()
{
  Test::Tester myTest;
  //... 


I'm getting that the constructor is private, so I'm a little stuck.
Well, you defined your constructor/destructor as private in both classes. Have you tried giving them public access?
Class Test is entirely public. I have made the Tester constructor public, and since that works, that's what I'm doing.

I'm trying to make my own iterator (the same wall we bang our heads against today :) )

So in my code, "Tester" is the iterator class. Having the public constructor allows for this:
1
2
3
4
int main()
{
  Tester myTest;
  //... 

Which make absolutely no sense (an iterator with no vector).
closed account (zb0S216C)
Perhaps this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct container
{
    container() : a__(0) { }

    friend struct sub_container;
    struct sub_container
    {
        void access_container_member(container &container_)
        {
            container_.a__ = 10;
        }
    };

    private:
        int a__;
};

int main()
{
    container container_;
    container::sub_container sub_;
    
    sub_.access_container_member(container_);
}

No?

Wazzak
That seems to work. It also gave me a hint that seems to solve my problem too:
1
2
3
4
5
6
7
8
class Test
{
       friend class Tester;
public:
	typedef Tester test;
	Test(){};
	~Test(){};
};

You declared here

class Tester
{
private:
friend class test;
Tester(){};
~Tester(){};
};


that class test is a friend of Tester. So the class test shall be defined in the same scope as class Tester. However you did not give its definition.

class test is not the same as Test::test.
Last edited on
Hmm, in my real file it is okay. (Slow day, had fun with my linked list)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
template <typename T>
class LinkedList
{
	//friend class Iterator<T>;

public:
	typedef Iterator<T> iterator;
	iterator begin(void) { return head; }
	iterator end(void) { return NULL; }

private:
	// Member Variables
	Node<T>* head;
	Node<T>* tail;
	unsigned size;

	// Member Functions

	void deleteList(void);

public:	
	// Construct/Destruct
	LinkedList(void) : head(NULL), tail(NULL), size(0) {}
	~LinkedList(void) { deleteList(); }

	// Member Functions
	bool empty(void) { return size == 0; }
	unsigned sizeOf(void) { return size; }
	void push_back(const T& data);
	void insert(const T& data, unsigned pos);
	void insert(const T& data, Iterator<T>& iter);
	void remove(unsigned pos);
	void remove(Iterator<T>& iter);
};



template <typename T>
class Iterator
{
	//Friends
	friend class LinkedList<T>;

private:
	// Member Variables
	Node<T>* nodePtr;

	// Construct/Destruct
	
	Iterator(Node<T>* nodePtr) : nodePtr(nodePtr) {}
public:
	Iterator(void) : nodePtr(NULL) {}	
	~Iterator(void) {}

	void operator=(const Node<T>* nodePtr);
	void operator++(void);
	void operator--(void);
	T& operator*(void);
	bool operator==(const Iterator<T>& iter);
	bool operator!=(const Iterator<T>& iter);

};


You may see that my last post didn't really solve it, and that I've reverted back to my public constructor (why spend so much time on one line?).

As a side, I've noticed my "--" and "++" overloads are only for the prefixed version (++iter;), how would I make a postfix version?
Last edited on
Topic archived. No new replies allowed.