Solved

Templates; making a class a friend

Posted on 2000-03-30
3
188 Views
Last Modified: 2010-04-10
I'm almost there (after getting the answer to my more critical error earlier)....

I have two template classes. I want to make one a friend of the other so it can see the private member variables.

ListNode.h
-----------
template <class T>
class ListNode {
  friend class List<T>;
}

List.h
---------
#include "ListNode.h"

template <class T>
class List {

}

What happens is I get a "syntax error : '<' " on the line

friend class List<T>;

I've tried including "List.h" in this file, no change.

So is this possible? If so, how? What am I screwing up?

Thanks!
0
Comment
Question by:gunn
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
arikka earned 50 total points
ID: 2669928
Declaration of class List should be prior ListNode, so in ListNode.h
write forward declaration:

template <class T> class List;  

template <class T> class ListNode
{
  friend class List<T>;
}
0
 

Author Comment

by:gunn
ID: 2670049
I could have swore I tried that!

Actually, what I tried was:

template <class T> class List<T>;

and so was getting an error on the '<'. Guess I should have tried that one last combination ;)

I'm all set now!
0
 

Author Comment

by:gunn
ID: 2670050
I could have swore I tried that!

Actually, what I tried was:

template <class T> class List<T>;

and so was getting an error on the '<'. Guess I should have tried that one last combination ;)

I'm all set now!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now