• Advertisement

Archived

This topic is now archived and is closed to further replies.

What is wrong w/ this? (Template/C++ Q.)

This topic is 6455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this code copied straight from a book (basically):
        

template <class T> 
class	SLLNode
{
	friend class SLL<T>;
protected:
	SLLNode<T>(T* pDataIn);

	SLLNode<T>	*pNext;
	T			*pData;
};

template <class T>
class	SLL
{
protected:
	SLLNode<T>	*pFirst,
				*pLast,
				*pCurrent;
	int			nMembers;
public:
			SLL();
	void	add(T*	pData);
	T*		current(void);
	void	nextNode(void);
	void	reset(void);
};
        
the compiler (VC++ 5.0) gives me a syntax error on the first '<' when I try to declare SLL as a friend (line 4). Its set up to compile as C++, and all this code is defined in a .h file (the listing above is sans member func definitions). So... whats up with this? It seems pretty straightforward, but it won't compile. I'd really appreciate any help. This thing is driving me nuts. Edited by - A. Buza on 6/20/00 4:02:56 PM Edited by - A. Buza on 6/20/00 4:04:00 PM

Share this post


Link to post
Share on other sites
Advertisement
I can''t remember much about templates, but... I think you need to declare the class "SLL" before using it as a friend class in "SLLNode". Try including the line "class SLL;" at the very beginning. I could be totally wrong, but hey give it a try

Share this post


Link to post
Share on other sites
you could just make all the members in SLLNode public and do away with the Friend bit.

since you are only acessing the SLLNode class via SLL anyway, you dont really need to hide all the data.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I am not positive, but i think the problem with it is that you are trying to use a class that has not been declared yet.

The SSLNode class is defined above SSL. That might cause a problem. If you try to add a friend class to SSL, it does not know about SSL at that point in the line.

Could be wrong


Share this post


Link to post
Share on other sites
Ah-ha! You hit the nail on the head (is that even a saying?). I did need to declare SLL before using it... seems obvious now . 'class prototyping' didn't really occur to me at the time.

Thanks for all of your suggestions!



Edited by - A. Buza on June 20, 2000 7:11:49 PM

Share this post


Link to post
Share on other sites
How odd. I''m sure I''ve declared friends without pre-announcing the class. The presence of the ''class'' keyword in the friend specification should be enough, I think. Maybe it just doesn''t like it for template classes, as I''m positive it''s worked before.

Share this post


Link to post
Share on other sites

  • Advertisement