C++ 编写模板类Liste-get问题
我的老师要求写一个模板类列表(就像STL一样) 我的代码可以编译,但会崩溃,但我找不到原因,有人能帮我吗? 我想可能有两个参数的构造函数有问题C++ 编写模板类Liste-get问题,c++,stl,C++,Stl,我的老师要求写一个模板类列表(就像STL一样) 我的代码可以编译,但会崩溃,但我找不到原因,有人能帮我吗? 我想可能有两个参数的构造函数有问题 -----Main.cpp------ #include "Liste.h" #include <iostream> int main() { Liste<int> l ; int x = 5 ; int y = 6 ; int z = 7 ; l.insert_first(y) ;
-----Main.cpp------
#include "Liste.h"
#include <iostream>
int main()
{
Liste<int> l ;
int x = 5 ;
int y = 6 ;
int z = 7 ;
l.insert_first(y) ;
l.insert_first(x) ;
l.insert_last(z) ;
std::cout << l << std::endl ;
}
-----Liste.h------
#pragma once
#include <stdio.h>
#include <iostream>
#include "NoeudListe.h"
template <class T> class Liste
{
private:
NoeudListe<T>* tete;
NoeudListe<T>* fin;
public:
Liste()
{
tete = new NoeudListe<T>(NULL,fin,NULL); //<-----I think the problem lies here beacause the val can`t be NULL? But I tried 0, still not work...
fin = new NoeudListe<T>(tete,NULL,NULL);
}
void insert_after (T& val, NoeudListe<T>* location)
{
NoeudListe<T>* local = new NoeudListe<T>(location,location->getNext(),val);
(*(location->getNext())).setPrev(local);
location->setNext(local);
}
void insert_first(T& val)
{
insert_after (val, tete);
}
void insert_last(T& val)
{
insert_after (val, fin->getPrev());
}
friend std::ostream& operator << (std::ostream& os,const Liste<T>& location)
{
NoeudListe<T>* local=(location.tete)->getNext();
while( local->getVal() != NULL )
{
os<< local->getVal() << "\n";
local=local->getNext();
}
return os;
}
};
-----NoeudListe.h------
#pragma once
template <class T> class NoeudListe
{
private:
NoeudListe* prev;
NoeudListe* next;
T val;
public:
NoeudListe(NoeudListe* p, NoeudListe* n, const T& v)
{
prev = p;
next = n;
val = v;
}
void setNext(NoeudListe* n){ next = n;}
void setPrev(NoeudListe* p){ prev = p;}
void setVal(const T& v){ val = v;}
NoeudListe* getNext(){ return next;}
NoeudListe* getPrev(){ return prev;}
T getVal(){ return val;}
};
----Main.cpp------
#包括“Liste.h”
#包括
int main()
{
李斯特l;
int x=5;
int y=6;
int z=7;
l、 首先插入_(y);
l、 首先插入_(x);
l、 插入_last(z);
标准::cout setNext(本地);
}
无效先插入(T&val)
{
在(val,tete)之后插入_;
}
最后一次无效插入(T&val)
{
在(val,fin->getPrev()之后插入_;
}
friend std::ostream&运算符getNext();
while(local->getVal()!=NULL)
{
osgetVal()getNext();
}
返回操作系统;
}
};
-----NoeudListe.h------
#布拉格语一次
模板类NoeudListe
{
私人:
NoeudListe*prev;
NoeudListe*下一步;
T值;
公众:
NoeudListe(NoeudListe*p、NoeudListe*n、const T&v)
{
prev=p;
next=n;
val=v;
}
void setNext(NoeudListe*n){next=n;}
void setPrev(NoeudListe*p){prev=p;}
void setVal(const T&v){val=v;}
NoeudListe*getNext(){return next;}
NoeudListe*getPrev(){return prev;}
T getVal(){return val;}
};
friend std::ostream和操作符getNext();
while(local->getVal()!=NULL)
{
osgetVal()getNext();
}
返回操作系统;
}
如果
local
为NULL,您不需要在这里进行测试(为什么要测试local->getVal()
是否为NULL?),因此您将在这里得到一个segfault。当您通过调试器运行代码时,它告诉您崩溃的原因是什么?@Sneftel,说实话,我仍然不知道如何使用调试器。。。。但是通过调试器运行它,我得到:this=0x41c30e,p=0xb927f8和tete,fin,n不可用。在第行:void setPrev(NoeudListe*p){prev=p;}之后,它会发出声音,就像你知道这个过程的下一步是什么一样,然后:学习使用调试器。@Sneftel还编程接收信号SIGSEGV,分段错误。第20行是void setPrev(NoeudListe*p){prev=p;}@Sneftel,问题是tete=newnoeudliste(NULL,fin,NULL)代码>这里fin不是init,谢谢你的评论,我将学习如何使用debuggerHi,谢谢你的回答。正如你所说,测试local->getVal()是否为NULL是不明智的,因此我将其改为“while(local->getNext()!=NULL)”,它仍然会崩溃,调试器给出了相同的结果:(调试器给出了什么?通过调试器运行它,我得到:this=0x41c30e,p=0xb927f8,tete,fin,n不可用。第行之后:void setPrev(NoeudListe*p){prev=p;},它将crashAlso程序接收到信号SIGSEGV,分段错误。在第20行是void setPrev(NoeudListe*p){prev=p;}问题是tete=new NoeudListe(NULL,fin,NULL);这里fin不是init,非常感谢您的回答!
friend std::ostream& operator << (std::ostream& os,const Liste<T>& location)
{
NoeudListe<T>* local=(location.tete)->getNext();
while( local->getVal() != NULL )
{
os<< local->getVal() << "\n";
local=local->getNext();
}
return os;
}