C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-19-2004, 03:51 PM   #1
Registered User
 
Join Date: May 2003
Posts: 82
Declare a template class as a friend?

I'm feeling really silly as this is just a syntax detail, but I have not managed to find a decent example on the web or in my textbooks.

How do you define a template class declared outside the class as a friend? I'm working on a linked list container class, and defining my List class as a friend of Iterator, which looks something like this:

iterator.h
Code:
#include "node.h"

template <class T>
class Iterator
{
	// declare class List as a friend somewhere?
	public:
		typedef *Node<T> nodePtr;

		T& operator*();
		bool operator==();
		//etc
	private:
		nodePtr dataNode;
		void setNext();
		void setPrev();
		void getNext();
		// etc accessors/mutators for node manipulation
}
list.h
Code:
#include "iterator.h"

template <class T>
class List
{
	public:
		typedef Iterator<T> iterator;

		void push_back(const T& val);	// functions need access to Iterator's 
		void push_front(const T& val);	// private methods to change sequence
		void insert(iterator pos);	// of list.
	private:
		iterator head;
		iterator tail;
};
They're may be some syntax errors. I don't have the code in front of me, so I'm just typing this in notepad from memory.

I looked at the STL list header, and they got around this buy declaring their iterator inside the list function, but I'm not sure thats appropriate for this project. I found plenty of examples on how to declare a non-template class as a friend, and I experimented with possible ways of writing a template version, but my compiler didn't like any of them. In case it effects the template syntax, I'm currenty using MS VC++ 6.0 SP 4.

Thank you,
AH_Tze
AH_Tze is offline   Reply With Quote
Old 05-19-2004, 04:15 PM   #2
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,903
Code:
template <class T>
class List
{
    friend class Iterator<T>;
...
};
gg
Codeplug is offline   Reply With Quote
Old 05-19-2004, 06:04 PM   #3
Registered User
 
Join Date: May 2003
Posts: 82
Just plugged that into Dev-C++, although I switched the friend, so within Iterator class definition it declares "friend class List<T>".

Compiles with an "List is not a template" error. After looking at non-template friend examples, I think I need some placeholder code in iterator.h which tells the compiler a class List will be defined, but I'm not sure how to write this for a template class.

I wrote a quicky test program in which class List tries to access class Iterator's private member function. Hopefully this will illustrate my problem.

main.cpp
Code:
#include "list.h"

int main(int argc, char *argv[])
{
    List<int> testList;
    int testVal;
    
    testVal = testList.getData();
  
  system("PAUSE");	
  return 0;
}
list.h
Code:
#ifndef LIST_H
#define LIST_H

#include "iterator.h"

template <class T>
class List
{
  
    private:
        Iterator<T> data;

    public:
                
        T getData() {
                return *(data.getdata());   // access private member of data
        }
};

#endif
iterator
Code:
#ifndef ITERATOR_H
#define ITERATOR_H

template <class T>
class Iterator
{
    //friend class List<T>; // compile error
    
    private:
        T* dataPtr;

        T* getdata() {
                return dataPtr;
        }

        public:
        Iterator() {
                dataPtr = NULL;
        }

};

#endif
AH_Tze is offline   Reply With Quote
Old 05-19-2004, 06:08 PM   #4
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
No declaration for List is present in iterator.h, so naturally the compiler won't recognize a friend declaration. You can either include list.h in iterator.h, or create a forward declaration for list in iterator.h to bring a declaration within scope.
__________________
My best code is written with the delete key.
Prelude is offline   Reply With Quote
Old 05-19-2004, 06:19 PM   #5
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,903
Code:
template <class T> class B; // forward decl.

template <class T>
class A
{
    T t;

public:
    void Test(const B<T> &b) {t = b.t;}
};//A

template <class T>
class B
{
    friend class A<T>; // friend decl.
    T t;
};//B
Now you have an example of a forward and friend template class declaration.

gg
Codeplug is offline   Reply With Quote
Old 05-19-2004, 06:20 PM   #6
Registered User
 
Join Date: May 2003
Posts: 82
ahh... forward declaration. I think that was what I was looking for.

In iterator.h, before the class, I added
Code:
template<class T>
class List;
And now it works like I wanted it to. The depressing thing is that I think I did that exact same thing earlier in VC++ 6.0 and it gave me an error. Or maybe it was just a typo on my part.

Either way, thank you Prelude and Codeplug.
AH_Tze is offline   Reply With Quote
Old 05-19-2004, 06:21 PM   #7
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>The depressing thing is that I think I did that exact same thing earlier in VC++ 6.0 and it gave me an error.
How is that depressing? VC++ 6.0 sucks and it's not your fault.
__________________
My best code is written with the delete key.
Prelude is offline   Reply With Quote
Old 05-19-2004, 07:38 PM   #8
Registered User
 
Join Date: May 2003
Posts: 82
Quote:
How is that depressing?
Its depressingly consistent. My data structs teacher is keeps accusing my of slamming my hand in the drawer by making all the assignments more complicated than necessary.
I've been trying to grok all of Accelerated C++, so I've been kinda out in left-field playing with generic programming, which was beyond the scope of the class.
I'm just thinking of the look on his face when I tell him I broke the compiler. But finals are next week, so all is good.

Thanks again for your help. After I got home I got my code running on dev-c++, so I should be set.
AH_Tze is offline   Reply With Quote
Old 05-19-2004, 07:44 PM   #9
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,903
I did the example in VC++ 6.0

gg
Codeplug is offline   Reply With Quote
Old 05-19-2004, 08:21 PM   #10
Registered User
 
Join Date: May 2003
Posts: 82
If you break it up into a driver and seperate headers for each class it no longer works.
AH_Tze is offline   Reply With Quote
Old 05-19-2004, 09:12 PM   #11
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,903
Code:
// a.h
template <class T> class B; // forward decl.

template <class T>
class A
{
    T t;

public:
    void Test(const B<T> &b) {t = b.t;}
};

***************************************************

// b.h
template <class T> class A; // forward decl.

template <class T>
class B
{
    friend class A<T>;
    T t;
};

***************************************************

// main.cpp
#include "b.h"
#include "a.h"

int main()
{
    A<int> a;
    B<int> b;

    a.Test(b);

    return 0;
}//main
Works fine.

gg
Codeplug is offline   Reply With Quote
Old 05-19-2004, 09:24 PM   #12
Registered User
 
Join Date: May 2003
Posts: 82
ah... you need two forward declarations.
/me takes double dose of Flinstone Kids Obvious Pills (tm)

This changes things. Danke.

EDIT:
wait, I spoke too soon.
debugging....

EDIT of the EDIT:
d'oh, typo. had an extra set of brackets on the forward declaration. But now both your example and my actual List class are working. My fault, not VC++'s.
Thank you again.

Last edited by AH_Tze; 05-19-2004 at 09:31 PM.
AH_Tze is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
error: template with C linkage michaels-r C++ Programming 3 05-17-2006 08:11 AM
template class default constructor problem kocika73 C++ Programming 3 04-22-2006 09:42 PM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM
Instantiating a template class NullS C++ Programming 11 02-23-2005 10:04 AM
Operator overloading in template classes moejams C++ Programming 5 07-21-2003 05:16 PM


All times are GMT -6. The time now is 07:45 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22