-2
\\\
#include<iostream>
using namespace std;
template<typename T> class List;
template<typename T> class Node{
    T data;
    Node<T> *link;
public:
    Node();
    Node(int a);
    friend class List<T>;
};
template<typename T> Node<T>::Node(){
    data=0;
    link=NULL;
}
template<typename T> Node<T>::Node(int a){
    data=a;
    link=NULL;
}
template<typename T> class List{
    Node<T> *head,*tail;
public:
    List();
    ~List();
    List & operator =(const List<T>& l);
    List<T>& operator+=(Node<T>& n);
    List<T>& operator+=(const List<T>& l);
    List<T> operator+( List<T>& l);
    void Printlist();
};
template<typename T> List<T>::List(){
    head=new Node<T>;
    tail=head;
}
template<typename T> List<T>::~List(){
    Node<T> *p=head,*q;
    while(p!=NULL){
        q=p->link;
        delete p;
        head=q;
        p=q;
    }
}
template<typename T> List<T> & List<T>::operator=(const List<T> & l){
    Node<T> *p=l.head->link,*q=head,*q2;
    while(p!=NULL){
        if(q->link!=NULL) q->link->data=p->data;
        else {q2=new Node<T>(p->data);
            q->link=q2;
            tail=q2;
        }
        p=p->link;
        q=q->link;
    }
    return *this;
}
template<typename T> List<T> & List<T>::operator+=(Node<T>& n){
    tail->link=&n;
    tail=&n;
    return *this;
}
template<typename T> void List<T>::Printlist(){
    Node<T> *p=head->link;
    while(p!=NULL){
        cout<<p->data<<' ';
        p=p->link;
    }
    cout<<endl;
}
template< typename T> List<T>& List<T>::operator+=(const List<T>& l){//redifinition of "+="
    Node<T> *p=tail,*q=l.head->link,*p2;
    while(q!=NULL){
        p2=new Node<T>(q->data);
        p->link=p2;
        tail=p2;
        p=tail;
        q=q->link;
    }
    return *this;
}
template<typename T> List<T> List<T>::operator+( List<T>& l){//"+"
    List<T>  p;
    p=*this;
    p+=l;
    return p;
}
int main(){
    List<int> A,B;
    int i=0;
    while(i<5){
        Node<int> *p,*q;
       p=new Node<int>(2*i+1);
        q=new Node<int>(2*i+2);
        A+=*p;
        B+=*q;
        i++;
    }
    cout<<"A:";
    A.Printlist();
    cout<<"B:";
    B.Printlist();
    cout<<"B copy A"<<endl;
    A=B;
    cout<<"A:";
    A.Printlist();
    cout<<"B:";
    B.Printlist();
    Node<int> *a=new Node<int>(12);
    cout<<"add one node to A"<<endl;
    A+=*a;
    cout<<"A:";
    A.Printlist();
    cout<<"C:";
    List<int> C;
    i=0;
    while(i<3){
        Node<int> *p;
       p=new Node<int>(10*i+20);
        C+=*p;
        i++;
    }
    C.Printlist();
    cout<<"A+C"<<endl;
    A+=C;
    cout<<"A:";
    A.Printlist();
    cout<<"C:";
    C.Printlist();
    cout<<"D=A+B+C"<<endl;
    List<int> D;
    D=A+B+C;
    cout<<"A:";
    A.Printlist();
    cout<<"B:";
    B.Printlist();
    cout<<"C:";
    C.Printlist();
    cout<<"D:";
    D.Printlist();
}
\\\

while I run the code above on either gcc or Xcode ,it turns out perfectly. the results are as bellow: A:1 3 5 7 9 B:2 4 6 8 10

A:2 4 6 8 10 B:2 4 6 8 10

A:2 4 6 8 10 12 C:20 30 40 A+C A:2 4 6 8 10 12 20 30 40 C:20 30 40 D=A+B+C A:2 4 6 8 10 12 20 30 40 B:2 4 6 8 10 C:20 30 40 D:2 4 6 8 10 12 20 30 40 2 4 6 8 10 20 30 40 Program ended with exit code: 0 while msvc could not run why?

ChrisMM
  • 7,552
  • 11
  • 27
  • 44
Zonaldo
  • 1
  • 1
  • msvc could not run? – Zonaldo May 19 '22 at 09:49
  • why? I need help – Zonaldo May 19 '22 at 09:49
  • Does the code *build* correctly? Without warnings? Even when you enable more warnings (for example build with `/W4` in MSVC)? – Some programmer dude May 19 '22 at 09:51
  • 2
    And have you tried to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to see what really happens? For example to catch possible crashes? – Some programmer dude May 19 '22 at 09:51
  • it builds correctly both in gcc and msvc... – Zonaldo May 19 '22 at 09:52
  • 4
    I also suggest you take a few steps back, invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and relearn the basics, because you do some mistakes that really should not be done at the level of your exercise. – Some programmer dude May 19 '22 at 09:54
  • yes I have tried it seems in msvc, one pointer just suddenly points to 0xFFFFFFFFFFFFFFFF – Zonaldo May 19 '22 at 09:55
  • 1
    Hint: Somewhere you save pointers to a *local* variable, a variable whose life-time will end when the function returns. Making the pointer immediately invalid. – Some programmer dude May 19 '22 at 09:56
  • could you plz point out some of my mistakes cause I am relatively fresh – Zonaldo May 19 '22 at 09:56
  • 1
    And that is happens to seemingly work when built by one compiler is purely a fluke, a case of *bad* luck (as many would say). This is unfortunately the nature of *undefined behavior*. – Some programmer dude May 19 '22 at 09:57
  • yes, I have considered that ,but the pointer become invalid right in a function – Zonaldo May 19 '22 at 09:58
  • 1
    Lastly (from me) I also implore you to think about the design. Why do you need to use nodes directly in your code? Shouldn't nodes only be handled internally by the list? Wouldn't it be better if you added *values* to the list, rather than nodes? That would solve some of the memory leaks you have, and probably the pointer to local variable issue as well. Hide how the list works internally from the users of the list. For example, do a function like `List& operator+=(T const& value);` – Some programmer dude May 19 '22 at 09:59
  • It's part of homework set... as the original main() function involves node – Zonaldo May 19 '22 at 10:01
  • 2
    Please don't tag all those different language versions. Those tags have a specific purpose. Removed. – Paul Sanders May 19 '22 at 10:15
  • Your class `List` violates the Rule Of Three. I didn't look further, but wouldn't be surprised if that's related to the problem. – aschepler May 19 '22 at 10:31

0 Answers0