Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Templates; making a class a friend

Posted on 2000-03-30
3
Medium Priority
?
193 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 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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