Solved

Templates; making a class a friend

Posted on 2000-03-30
3
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

729 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