Problem with pointer

I'm a beginner in c++, and I try to solve a polynomial problem with linked list, when I execute the program it stop executing and a window shows up saying that 'main.exe ceased to function'
what's the problem here ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  
class Monome{
public:
    int cof;
    int deg;
    Monome *suivant;
 
    Monome(int cof, int deg){
 this->cof=cof;
 this->deg=deg;
 }
 
};
 
class Polynome{
static int conteur;
int id;
public : int taille;
Monome *debut, *fin;
 
//-----------------constructeur-----------------------------
public: Polynome(){
    id=conteur;
    conteur++;
    debut=fin=NULL;
    taille=0;
}
 
void ajouter(int a, int b){
 
Monome *element;
if(taille==0){
       element->cof=a;
        element->deg=b;
        element->suivant=NULL;
        debut=element;
        fin=element ;
 
        //taille ++;
    }
}
}

Thank you
You need to allocate your pointers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Monome {
public:
    // ...
    Monome(int cof, int deg) {
        // ...
        this->suivant = nullptr; // initialise everything in constructor
    }
};

class Polynome {
    // ...

public:
    // ...

    ~Polynome() {
        // you'll need to clean up; call delete on each element in here
    }

    void ajouter(int a, int b) {
        Monome* element = new Monome(a, b);
        // add it to your linked list
    }
};

That should give you an idea on what to do.
Last edited on
Thank you so much
Topic archived. No new replies allowed.