dragondaddy63 2022-05-06 14:33 采纳率: 80%
浏览 17
已结题

为什么会反复调用构造函数?

#include<iostream>
using namespace std;

template<class T>
class list{
    public:
        list()
        {
            cout<<"^";
            head=new list<T>;
            length=0;
        }
        list(T value,list<T>* next1)
        {
            data=value;
            next=next1;
        }
        bool Insert(T value,int i);
        int Findvalue(T value);
        list<T>* Findi(int i);
        bool Remove(int i);
        void display();
        T Display()
        {
            return data;
        }
        list<T>* Head()
        {
            return head;
        }
        int Length()
        {
            return length;
        }
    protected:
        list<T>* head;
        T data;
        list<T>* next;
        int length;
}; 

template<class T>
list<T>* list<T>::Findi(int i)
{
    if(i<0||i>length)
        return NULL;
    if (i==0) 
        return head;
    int j=1;
    list<T>* p=head->next;
    if (j<i&&p!=NULL)
    {
        p=p->next;
        j++;
    }
    cout<<"Findi ok!"<<endl;
    return p;
}

template<class T>
bool list<T>::Insert(T value,int i)
{
    cout<<"Insert 1 ok!"<<endl;
    list<T>* p=Findi(i-1);
    if (p==NULL)
    {
        cout<<"so sad~"<<endl;
        return false;
    }
    list<T>* newp = new list<T>(value,p->next);
    if (newp==NULL)
        return false;
    p->next=newp;
    length++;
    cout<<"Insert ok!"<<endl;
    return true;
}

template<class T>
int list<T>::Findvalue(T value)
{
    list<T>* p=head->next;
    int i=1;
    while (i<length&&p->data!=value)
    {
        p=p->next;
        i++;
    }
    cout<<"Findvalue ok!"<<endl;
    return i;
}

template<class T>
bool list<T>::Remove(int i)
{
    list<T>* p=Findi(i-1),*q;
    if (p==NULL)
        return false;
    q=p->next;
    p->next=q->next;
    delete q;
    length--;
    cout<<"Remove ok!"<<endl;
    return true;
}

template<class T>
void list<T>::display()
{
    list<T>* p=head->next;
    int i=1;
    while (i<=length)
    {
        cout<<p->data<<" ";
        i++;
        p=p->next;
    }
    cout<<"display ok!"<<endl;
    cout<<endl;
}

int main()
{
    list<int> list1;
    int num;
    int i=1,j=0;
    while (1)
    {
        cin>>num;
        list1.Insert(num,i);
        i++;
        cout<<"ok ";
        if (cin.get()=='\n')
        {
            cout<<"break!"<<endl;
            break;
        }
    }
    int num1;
    cin>>num1;
    j=list1.Findvalue(num1)-1;
    list1.Remove(j);
    list1.display();
}

编译结果是重复出现^符号,且无法输入数据。当我把构造函数中的new list那里改为null以后,就可以输入数据了。

  • 写回答

1条回答 默认 最新

  • JabinQu 2022-05-06 15:09
    关注

    你已经找到问题点了,就是构造中不断的在new,不断进入构造,一直重复

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 5月14日
  • 已采纳回答 5月6日
  • 创建了问题 5月6日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值