| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 372 人关注过本帖
标题:这程序到底哪里错了,编译不通过
只看楼主 加入收藏
xzy199999
Rank: 4
等 级:业余侠客
帖 子:326
专家分:286
注 册:2010-11-19
结帖率:92.45%
收藏
 问题点数:0 回复次数:2 
这程序到底哪里错了,编译不通过
#include <iostream>
using namespace std;
template <class T> class node
{
public:
    node():id(0),next(NULL){}
    T id;
    node *next;
};

class link
{
public:
    link():m_head(NULL),m_count(0){}
    int insert(int id,int pos);//向pos位置前添加结点,pos不合法返回-1,否则0
    void print_all();//遍历链表并打印元素
private:
    node *m_head;//链表头指针
    int m_count;//结点个数
};
int link::insert(int id,int pos)
{
    node *s,*ss,*news;
    int num;
    news= new node;
    news->id=id;
    if((m_head==NULL)&&(pos==0))
    {
        news->next=NULL;
        m_head=news;
        return 0;
    }
    for(s=ss=m_head,num=1;(s!=NULL)&&(num<=pos);ss=s,s=s->next,num++)
    if(s==NULL)
    {
        printf("i value exceed\n");
        return -1;
    }
    if(ss!=s)
    {
        news->next = s;
        ss->next=news;        
    }
    else
    {
        news->next=s;
        m_head=news;
    }
    return 0;
}

void link::print_all()
{
    node *s;
    for(s=m_head;s!=NULL;s=s->next)
    {
        printf("->%d\n",s->id);
    }
}

int main()
{
    link obj;
    for(int i=0;i<5;i++)
    {
        obj.insert(i,1);
    }
    return 0;
}
搜索更多相关主题的帖子: class private include public 
2011-06-23 19:49
xzy199999
Rank: 4
等 级:业余侠客
帖 子:326
专家分:286
注 册:2010-11-19
收藏
得分:0 
#include <iostream>
using namespace std;
template <class T> class node
{
public:
    node():id(0),next(NULL){}
    T id;
    node *next;
};

class link
{
public:
    link():m_head(NULL),m_count(0){}
    int insert(int id,int pos);//向pos位置前添加结点,pos不合法返回-1,否则0
    void print_all();//遍历链表并打印元素
private:
    node<int> *m_head;//链表头指针
    int m_count;//结点个数
};
int link::insert(int id,int pos)
{
    node<int> *s,*ss,*news;
    int num;
    news= new node<int>;
    news->id=id;
    if((m_head==NULL)&&(pos==0))
    {
        news->next=NULL;
        m_head=news;
        return 0;
    }
    for(s=ss=m_head,num=1;(s!=NULL)&&(num<=pos);ss=s,s=s->next,num++)
        if(s==NULL)
        {
            printf("i value exceed\n");
            return -1;
        }
        if(ss!=s)
        {
            news->next = s;
            ss->next=news;        
        }
        else
        {
            news->next=s;
            m_head=news;
        }
        return 0;
}

void link::print_all()
{
    node<int> *s;
    for(s=m_head;s!=NULL;s=s->next)
    {
        printf("->%d\n",s->id);
    }
}

int main()
{
    link obj;
    for(int i=0;i<5;i++)
    {
        obj.insert(i,1);
    }
    return 0;
}

我是初学者
2011-06-27 16:18
lovejj23
Rank: 2
等 级:论坛游民
帖 子:20
专家分:41
注 册:2011-4-17
收藏
得分:0 
你是为了重载NODE吗 还写个类模板
2011-07-04 21:17
快速回复:这程序到底哪里错了,编译不通过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013983 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved