C++ 获取模板类列表/节点程序中的错误

C++ 获取模板类列表/节点程序中的错误,c++,class,templates,linked-list,C++,Class,Templates,Linked List,我正在尝试使用一个程序来创建和执行链表上的操作 这个程序按原样工作和编译,但我必须创建一个模板版本的程序,可以处理任何类型的数据(int、float、char)。程序中有两个类,NodeSLList类和IntNode类 我已经尽了最大努力创建了这两个类的模板版本,同时修改了我认为应该修改的成员声明和定义,但就我的一生而言,我无法摆脱其中的一些错误。我正在阅读和重读我关于模板和模板类的笔记,在网上搜寻更好的解释,并将在发布时继续工作,但同时我非常感谢任何帮助 有两个头文件和一个源代码文件 IntN

我正在尝试使用一个程序来创建和执行链表上的操作

这个程序按原样工作和编译,但我必须创建一个模板版本的程序,可以处理任何类型的数据(int、float、char)。程序中有两个类,NodeSLList类和IntNode类

我已经尽了最大努力创建了这两个类的模板版本,同时修改了我认为应该修改的成员声明和定义,但就我的一生而言,我无法摆脱其中的一些错误。我正在阅读和重读我关于模板和模板类的笔记,在网上搜寻更好的解释,并将在发布时继续工作,但同时我非常感谢任何帮助

有两个头文件和一个源代码文件

IntNode.h:

///////////////////////////////////////////////////////////////////////
// class IntNode
//
// Description: represents the data that we want to store in our 
//              linked list
///////////////////////////////////////////////////////////////////////
#ifndef __INT_NODE__
#define __INT_NODE__

#include <iostream>

using std::ostream;


// class declaration
template <typename N>
class IntNode {

// make the following friends of this class in order that they be
// able to directly access private data of this container class


    friend void NodeSLList_Test(void);
    template <typename N> friend class NodeSLList<N>;
    friend class TSArray;
    friend ostream& operator<<(ostream &, NodeSLList<N> &);

public:

///////////////////////////////////////////////////////////////////////
// ostream operator
// 
// Description: provide overloaded ostream operator
// Input: reference to ostream object
//        reference to IntNode object to output
// Output: none
// Returns: reference to same ostream operator for cascading
///////////////////////////////////////////////////////////////////////
    friend ostream& operator<<(ostream & out, IntNode<N> & n)
    {
        out << "[" << n.row << "]"
            << "[" << n.col << "]"
            << "[" << n.depth << "]:"
            << n.data;
        return out;
    }


///////////////////////////////////////////////////////////////////////
// default constructor
// 
// Description: provide default construction of an IntNode object
// Input: row of array
//        column of array
//        depth of array
//        initial value of node
//        pointer to next IntNode
// Output: none
// Returns: reference to same ostream operator for cascading
///////////////////////////////////////////////////////////////////////
    IntNode(int inRow=0, int inCol=0, int inDepth=0, N inData, IntNode<N> *in = 0)
    {
        data = inData; 
        next = in;
        row = inRow; 
        col = inCol; 
        depth = inDepth;
    }

///////////////////////////////////////////////////////////////////////
// GetRow
// 
// Description: return row member
// Input: none
// Output: none
// Returns: row member
///////////////////////////////////////////////////////////////////////
    const int GetRow() const
    {
        return row;
    }

///////////////////////////////////////////////////////////////////////
// GetColumn
// 
// Description: return column member
// Input: none
// Output: none
// Returns: column member
///////////////////////////////////////////////////////////////////////
    const int GetColumn() const
    {
        return col;
    }


///////////////////////////////////////////////////////////////////////
// GetDepth
// 
// Description: return depth member
// Input: none
// Output: none
// Returns: depth member
///////////////////////////////////////////////////////////////////////
    const int GetDepth() const
    {
        return depth;
    }


///////////////////////////////////////////////////////////////////////
// GetData
// 
// Description: return data member
// Input: none
// Output: none
// Returns: data member
///////////////////////////////////////////////////////////////////////
    const N GetData() const
    {
        return data;
    }


private:

///////////////////////////////////////////////////////////////////////
// row
// 
// this variable holds the row of the array element (i.e first demension)
///////////////////////////////////////////////////////////////////////
    int row;

///////////////////////////////////////////////////////////////////////
// column
// 
// this variable holds the column of the array element (i.e 2nd demension)
///////////////////////////////////////////////////////////////////////
    int col;

///////////////////////////////////////////////////////////////////////
// depth
// 
// this variable holds the column of the array element (i.e 3rd demension)
///////////////////////////////////////////////////////////////////////
    int depth;

///////////////////////////////////////////////////////////////////////
// data
// 
// this variable holds the actual data at the array element
///////////////////////////////////////////////////////////////////////
    N data;


///////////////////////////////////////////////////////////////////////
// column
// 
// this variable holds the column of the array element (i.e 2nd demension)
///////////////////////////////////////////////////////////////////////
    IntNode<N> *next;
};


#endif __INT_NODE__
///////////////////////////////////////////////////////////////////////
//类IntNode
//
//描述:表示要存储在数据库中的数据
//链表
///////////////////////////////////////////////////////////////////////
#ifndef\uuu INT\u节点__
#定义_INT_节点__
#包括
使用std::ostream;
//类声明
模板
类IntNode{
//结交本班的下列朋友,以便他们
//能够直接访问此容器类的私有数据
friend-void-NodeSLList_检验(void);
模板好友类节点列表;
朋友班沙雷;
friend ostream&operatornext;
tmp->data=node.data;
tmp->col=node.col;
tmp->row=node.row;
}
///////////////////////////////////////////////////////////////////////
//销毁清单
// 
//描述:取消分配链表的所有内存
//输入:无
//输出:重置链表
//退货:无
///////////////////////////////////////////////////////////////////////
作废销毁清单()
{
//虽然名单上没有empy
//继续移除头部节点并制作
//下一个节点是头部节点
对于(IntNode*p;!IsEmpty();)
{
p=头部->下一步;
删除标题;
水头=p;
}
头=尾=0;
}
私人:
IntNode*头,*尾;
};
#endif INT_链接列表
TestDriver.cpp:

// TEST DRIVER. Only used to test the class.
// Activated by defining TEST_DRIVER in the 
// Project - Settings - C/C++ - Preprocessor definitions
#ifdef LIST_DRIVER

#include <stdlib.h>
#include "NodeSLList.h"
#include "IntNode.h"



template <typename N>
void NodeSLList_template_Test()
{

    NodeSLList_template<N> s;
    IntNode_template<N> temp;

    IntNode_template<N> n1(1,1,1,10);
    IntNode_template<N> n2(1,2,1,20);
    IntNode_template<N> n3(1,3,1,30);
    IntNode_template<N> n4(1,4,1,40);
    IntNode_template<N> n5(1,5,1,50);

    cout << "Testing addToHead() operation" << endl;
    s.AddToHead(n5);
    s.AddToHead(n4);
    s.AddToHead(n3);
    s.AddToHead(n2);
    s.AddToHead(n1);

    cout << s << endl;

    cout << "\nTesting GetSize() operation" << endl;
    cout << "list contains " << s.GetSize() << " node(s)" << endl;

    cout << "\nTesting DeleteFromHead() operation" << endl;
    temp = s.DeleteFromHead();
    cout << "node retrieved " << temp << endl;
    cout << s << endl;

    cout << "\nTesting DeleteFromTail() operation" << endl;
    temp = s.DeleteFromTail();
    cout << "node retrieved " << temp << endl;
    cout << s << endl;

    cout << "\nTesting RetrieveNode() operation" << endl;
    temp = s.RetrieveNode(0);
    cout << "node retrieved (should be first node) " << temp << endl;
    temp = s.RetrieveNode(50);
    cout << "node retrieved (should be last node) " << temp << endl;
    temp = s.RetrieveNode(2);
    cout << "node retrieved (should be 2nd node) " << temp << endl;
    pause();

    cout << "Adding n3 to head" << endl;
    cout << "Adding n2 to head" << endl;
    s.AddToHead(n3);
    s.AddToHead(n2);
    cout << "List is now: " << s << endl;
    cout << "\nTesting DeleteNode() operation" << endl;
    temp = s.DeleteNode(50);
    cout << "node deleted (should be last node) " << temp << endl;
    cout << s << endl;
    temp = s.DeleteNode(3);
    cout << "node deleted (should be 3rd node) " << temp << endl;
    cout << s << endl;

    cout << "Test SsEmpty() operation" << endl;
    cout << (s.IsEmpty() ? "List IS Empty\n" : "List NOT Empty\n");

    cout << "\nTesting UpdateNode() operation (updating 3rd node with [10][20][30]:500)" 
        << endl;
    temp.row = 10;
    temp.col = 20;
    temp.depth = 30;
    temp.data = 500;
    s.UpdateNode(3,temp);
    cout << s << endl;

    pause();

    cout << "\nTesting the ability to delete nodes from head, even" << endl
        << "after list is empty" << endl
        << "Should recieve 2 errors" << endl;
    temp = s.DeleteFromHead();
    temp = s.DeleteFromHead();
    temp = s.DeleteFromHead();
    temp = s.DeleteFromHead();
    temp = s.DeleteFromHead();

    cout << "\nTest IsEmpty() operation" << endl;
    cout << (s.IsEmpty() ? "List IS Empty\n" : "List NOT Empty\n");

    cout << "\nTesting DestroyList() operation" << endl;
    s.AddToHead(n3);
    s.AddToHead(n2);
    s.AddToHead(n1);
    cout << s << endl;
    cout << "calling DestoryList()" << endl;
    s.DestroyList();
    cout << s << endl;

}

void pause()
{
    cout << "Press RETURN to continue" << endl;
    cin.get();
    system("cls");
}


void pause();

void main(void)
{
    NodeSLList_template_Test<int>();
}



#endif
//测试驱动程序。仅用于测试类。
//通过在中定义测试驱动程序激活
//项目-设置-C/C++-预处理器定义
#ifdef列表驱动程序
#包括
#包括“NodeSLList.h”
#包括“IntNode.h”
模板
void NodeSLList_template_Test()
{
NodeSLList_模板;
IntNode_模板温度;
IntNode_模板n1(1,1,1,10);
IntNode_模板n2(1,2,1,20);
IntNode_模板n3(1,3,1,30);
IntNode_模板n4(1,4,1,40);
IntNode_模板n5(1,5,1,50);

您可能有一个循环依赖项。
IntNode
需要声明
NodeSLList
,反之亦然

另一方面,带有前导下划线或两个相邻下划线的名称是为实现保留的,因此您应该对此进行更改

#ifndef __INT_NODE__
差不多

#ifndef INT_NODE_H_
#ifndef __INT_NODE__
#ifndef INT_NODE_H_