注册 登录
编程论坛 C++教室

请问类模板问题

ou1111 发布于 2010-11-11 16:25, 772 次点击
程序代码:
#include<iostream.h>
template <class T>
struct node
{
  T data;
  struct node *next;
};

class list
{private:
  node *head,*tail;
public:
    list()
    {head=NULL;tail=NULL;}
    void insert(T x)//为什么说没有定义T呢
    {
      node *p;
      p=new node;
      p->data=x;
      p->next=NULL;
      if(head==NULL)
      {head=p;p->next=tail;}
      else
      { p->next=head;
      head=p;}   
    }
    void show()
    {
      node *p;
      p=head;
      while(p)
      {
        cout<<p->data;
        p=p->next;
      }   
    }
    ~list()
    {
      node *p;
      while(head)
      {
        p=head;
        head=head->next;
        delete p;
      }
    head=NULL;
    tail=NULL;   
    }

};

10 回复
#2
m21wo2010-11-11 18:51
程序代码:

#include<iostream>
using namespace std;
template <class T>
struct node
{
  T data;
  struct node<T> *next;
};
template <class T >
class list
{
private:
  node<T> *head,*tail;
public:
    list()
    {head=NULL;tail=NULL;}
    void insert(T x)//为什么说没有定义T呢
    {
      node<T> *p;
      p=new node;
      p->data=x;
      p->next=NULL;
      if(head==NULL)
      {head=p;p->next=tail;}
      else
      { p->next=head;
      head=p;}  
    }
    void show()
    {
      node<T> *p;
      p=head;
      while(p)
      {
        cout<<p->data;
        p=p->next;
      }  
    }
    ~list()
    {
      node<T> *p;
      while(head)
      {
        p=head;
        head=head->next;
        delete p;
      }
    head=NULL;
    tail=NULL;  
    }

};
int main()
{
    list<int> f;
}

#3
ou11112010-11-11 19:07
回复 2楼 m21wo
只有本站会员才能查看附件,请 登录

还是同样的错误
#4
m21wo2010-11-11 22:25
我帮你很改了几处啊!上面没标出来啊!我这没错误啊!肯定没仔细看
#5
玩出来的代码2010-11-11 23:13
若2楼的代码在你的编译器中还是错误的,我想LZ或许应该换一个编译器。
#6
ou11112010-11-12 08:19
回复 4楼 m21wo
只有本站会员才能查看附件,请 登录


还是有错呀。。。。。。。难道是VC6.0不能跟win7兼容问题???????????
#7
zhoufeng19882010-11-12 10:06
Have a try.
程序代码:
/*------------------------------------------

 取消TEST_MAIN定义则关闭测试运行。

 ------------------------------------------
*/
#define TEST_MAIN
//#undef TEST_MAIN

#ifdef TEST_MAIN
    typedef char * pcstr;
#endif
// 测试链表模板

#include<iostream.h>

template<class T>
struct node
{
  T data;
  node<T> *next;
};

template< class T>
class list
{
private:
  node<T> *head,*tail;
public:
    list()
    {
        head = NULL;
        tail = NULL;
    }

    void insert(T x)        //为什么说没有定义T呢
    {
      node<T> *p;
      p = new node<T>;
      p -> data = x;
      p -> next = NULL;
      if( head == NULL)
      {
        head = p;
        p -> next = tail;
      }
      else
      {
        p -> next = head;
        head = p;
      }  
    }

    void show()
    {
      node<T> *p;
      p = head;
      while(p)
      {
        cout<<p->data;
        p=p->next;
      }  
    }

    ~list()
    {
        node<T> *p;
        while(head)
        {
            p = head;
            head=head->next;
            delete p;
          }

        head=NULL;
        tail=NULL;  
    }
};

#ifdef TEST_MAIN
   
int main()
{
    list<pcstr> strs;
    strs.insert( "C Program\n");
    strs.insert( "Cpp Program\n");
    strs.insert( "C# .net Program\n");
    strs.insert( "Java Program\n");

    strs.show();

    return 0;
}

#endif

#8
ou11112010-11-12 10:31
我找出错误了,2楼的程序
  node<T> *head,*tail;
public:
    list()
    {head=NULL;tail=NULL;}
    void insert(T x)//为什么说没有定义T呢
    {
      node<T> *p;
      p=new node;//这里应该是p=new node<T>
      p->data=x;
      p->next=NULL;
      if(head==NULL)
      {head=p;p->next=tail;}
      else
      { p->next=head;
      head=p;}  
    }

学习了,感谢大家的帮助!!!!!!!!!!


#9
missiyou2010-11-12 11:04
/*
*
*/

#include <iostream>
using namespace std;

//Node 结点
template <class T>
struct Node
{
    T data;
    struct Node *next;
    Node():next(NULL){}
    Node(Node *pNext, T &x):next(pNext), data(x){}
};

template <class T>
class List
{   
    public:
    typedef Node<T>* NodePtr;
    List():head(NULL), tail(NULL){ }
    void Insert(T x)
    {
        NodePtr pNode;
        if (NULL == head)
            pNode = new Node<T>(tail, x);
        else
            pNode = new Node<T>(head, x);
        head = pNode;
    }
    void Show()
    {
        NodePtr pNode;
        pNode = head;
        while(pNode)
        {
            cout << "data:" << pNode->data << endl;
            pNode = pNode->next;
        }// while pNode != Null
    }
    //pass
    private:
    //pass
        NodePtr head, tail;
};

int main()
{
    List<char*> strList;
    strList.Insert("hello");
    strList.Insert("world");
    strList.Insert("Insert");
    strList.Insert("endl");
    strList.Show();
}
本人很垃圾, 好好学习! 借你代码,改了改
#10
zhoufeng19882010-11-12 14:59
回复 9楼 missiyou
不要说自己垃圾撒,你的代码风格还是很好的,看着很舒服。绝对不是垃圾哈。
#11
八画小子2010-11-13 02:08
前面几位的编程风格和习惯应该值得我们学习。经常帮朋友和同学修改程序,看到他们糟糕的编程习惯就感觉头大。没有注释,所有语句都是从最左面开始的,没有缩减,分不出层次感来,60%的时间是帮他们重新修改风格。希望以后发上来的源代码是这种比较优秀的风格
1