Template issue - what's wrong with this line...?

Posted on 2000-02-14
Last Modified: 2011-04-14
I am trying to compile some sample code and am getting the error:

   "..error C2059: syntax error : '<' "

on the line:

    friend class CList<NodeType>;

The entire h file Code:

//  File Name
//      TEMPLATE.H
//  Description
//  Author
//      Les Thaler
//  Revision: 1.00
// Copyright (c) 1995 Microsoft Corporation. All rights reserved.
#ifndef _TEMPLATE_H_
#define _TEMPLATE_H_

#include <windows.h>
#include <stdio.h>
#include <mapidefs.h>

template <class NodeType> class CNode;

template <class NodeType>
class CNode
    friend class CList<NodeType>;      // Original line as found in the sample
        CNode(const NodeType & Type);
        CNode     * m_pNext;        //
        NodeType    m_Item;         // variable length, put last

template <class NodeType>
class CList
        BOOL       WINAPI Insert(const NodeType & val);
        BOOL       WINAPI InsertAsc(const NodeType & val);

        BOOL       WINAPI Delete(const NodeType &,NodeType ** ret);
        NodeType * WINAPI Find(const NodeType &SearchTarget);
        // same as Walk but doesn't copy the node
        NodeType * WINAPI Scan(BOOL bWalking = 1);
        // same as Scan but returns a copy of the node
        NodeType * WINAPI Walk(BOOL bWalking = 1);
        ULONG      WINAPI Size();
        NodeType * WINAPI Dequeue();
        VOID       WINAPI Destroy();
        BOOL       WINAPI Queue(const NodeType & val);
        CList<NodeType> & operator -=(CList<NodeType> & Deletions);
        CList<NodeType> & operator +(CList<NodeType> & Suffix);
        CNode<NodeType> * m_pHead,
                        * m_pTemp,
                        * m_pTail;      // used by Walk
        ULONG             m_ulCnt;



// End of file for TEMPLATE.H

Question by:jhattingh
  • 3
  • 2
LVL 22

Accepted Solution

nietod earned 50 total points
ID: 2518417
A friend declaration is not a true declaration, so "CList<>" is not defined at that point.  Add a forward declaration like

template <class NodeType> class CList;

before the friend declatation (outside of the class.)

Let me know if you have any questions.

Expert Comment

ID: 2518423
The problem is just that the compiler hasn't seen the template CList class when trying to compile CNode.

I think the forward declaration above the CNode class (which is redundant)should really be for CList, i.e. change:

template <class NodeType> class CNode;


template <class NodeType> class CList;



Expert Comment

ID: 2518427
Sorry, nietod, I didn't see your comment.  I have withdrawn my answer (didn't know you could do that) since you got there first.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 2518467
Wasn't mine posted as an answer?  I thought it was?

>> I have withdrawn my answer (didn't
>> know you could do that)
It was added in the latest version for this very reason and for cases where experts realize their answer is incorrect.  together with the accept comment as answer link it should make for speedier resolutions of "issues" in questions and without involving customer support personal as much.

Author Comment

ID: 2518526
It was posted as a comment
LVL 22

Expert Comment

ID: 2518547

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

726 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