私は一方向リストを実装しようとしています。 m3 = m1 + m2コマンドがmain関数で2倍になるまで、すべてが完璧に機能します。私がデバッグしていたとき、オーバーロードされた=演算子では、破棄が発生した後、o1オブジェクトに割り当てられた値が消えていたことに気付きました。デストラクタ、または演算子に何か問題があるかどうかは分かりません。オーバーロード演算子=、クラス内の面倒なデストラクタC++
#include <iostream>
using namespace std;
template <class T>
class Element;
template <class T>
class List{
friend class Element<T>;
Element<T> *head;
public:
List(){
cout<<"konstruktor"<<endl;
head=NULL;
}
~List() {
Element<T> *tmp = head;
cout << "destruktor" << endl;
while (tmp) {
//tmp = tmp->next;
delete head;
head = tmp;
}
}
friend istream &operator>>(istream &p, List<T> &o1){
Element<T>* new_ele;
Element<T>* it;
it=o1.head;
new_ele=new Element<T>;
p>>new_ele->value;
new_ele->next=NULL;
if (o1.head==NULL){
o1.head=new_ele;
}
else{
while (it->next!=NULL){
it=it->next;
}
it->next=new_ele;
}
return p;
}
friend ostream &operator<<(ostream &s, List<T> &o1){
Element<T>* it;
it=o1.head;
while(it){
s<<it->value<<" ";
it=it->next;
}
return s;
}
List <T> &operator=(const List<T> &o1){
if (this==&o1){
return *this;
}
Element<T> *it1, *it2, *itc;
this->~List();//this is the where everything goes haywire
itc=head;
it1=o1.head;
while(it1){
itc=new Element<T>;
if (!head) head=it1;
itc->next=NULL;
itc->value=it1->value;
it1=it1->next;
itc=itc->next;
}
return *this;
}
List<T> &operator+(List<T> &o1){
if(o1.head==NULL){
return *this;
}else if(head==NULL){
return o1;
}
static List<T> res=*this;
Element<T> *it;
it=res.head;
while(it->next) {
it = it->next;
}
Element <T> *o1_it=o1.head;
while(o1_it){
Element<T> *copy;
copy=new Element<T>;
copy->next=NULL;
copy->value=o1_it->value;
it->next=copy;
it=it->next;
o1_it=o1_it->next;
}
return res;
}
int length_list(){
Element<T> *it;
int count_elements=0;
it=head;
while(it->next){
count_elements++;
it=it->next;
}
return count_elements;
}
void bubblesort_List(){
Element<T> *it;
for(int i=0; this->length_list() > i;i++){
it = head;
while (it->next) {
if (it->next->value < it->value) {
T tmp = it->value;
it->value = it->next->value;
it->next->value = tmp;
}
it = it->next;
}
}
}
};
template <class T>
class Element{
friend class List<T>;
friend istream &operator>>(istream &p, List<T> &o1);
friend ostream &operator<<(ostream &s, List<T> &o1);
Element<T> *next;
T value;
public:
Element(){
next=NULL;
}
};
int main(){
List<int> m1, m2, m3;
cin>>m1>>m1>>m1;
cin>>m2;
m3=m1+m2;
m3=m1+m2;
cout<<m3<<endl;
return 0;
}
[mcve]にコードを減らすと、問題が見つかる可能性があります。もしそうでなければ、あなたはここで尋ねる合理的な質問をするでしょう。 –
デストラクタを明示的に呼び出さないでください。まれな例外がありますが、あなたの人生でそれらを満たすことはほとんどありません。 – Slava