Question

Je ne peux donc pas comprendre pourquoi mon opérateur d'insertion ne fonctionne pas pour ma classe de liste. Je l'ai regardé pendant un certain temps et je pense que la syntaxe est correcte pour la surcharge. Pas sûr sur celui-ci. Des conseils sur les raisons pour lesquelles cela ne fonctionne pas ?? Voici le code:

Edit: a changé de code pour ce qu'il est actuellement.

Désolé, le problème spécifiquement maintenant est que je ne peux pas le faire imprimer quoi que ce soit, ses impressions simples et sa ligne vide.

Voici le conducteur:

#include <iostream>
#include "polynomial.h"

using namespace std;

int main(){
Polynomial* poly = new Polynomial();    

poly->set_coefficient(3,2);

poly->set_coefficient(0,2);

poly->set_coefficient(3,1);

cout << "trying to print data" << endl;
cout << *poly << endl;    
return 0;   
}

Voici l'en-tête:

#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_

#include <iostream>

class Polynomial {

public:

struct PolyNode {
    int coefficient, degree;
    struct PolyNode* next;      
    PolyNode(int c, int d, PolyNode* n): coefficient(c),degree(d),next(n){}
};

PolyNode* firstTerm;
Polynomial(): firstTerm(0) {} 

struct PolyNode* get_first(){
    return firstTerm;
}


//makes the term with degree d have a coefficient of c
void set_coefficient(int c, int d);     

~Polynomial();  

friend std::ostream& operator<<(std::ostream& o, const Polynomial& p);          
};

#endif

Voici la mise en œuvre:

#include "polynomial.h"
#include <iostream>
#include <ostream>

using namespace std;

void Polynomial::set_coefficient(int c, int d){
PolyNode* start = firstTerm;
if(c != 0 && firstTerm == 0)    
    firstTerm = new PolyNode(c,d,NULL);
else{   
    cout << "Entered set_coefficient()" << endl;
    while(start->degree != d && start->next != NULL){
        cout << "Inside set_coefficient() while loop" << endl;          
        start = start->next;        
    }       
    if(c != 0 && start == 0)    
        start = new PolyNode(c,d,0);
    else if(c!= 0 && start != 0)
        start->coefficient = c;
    else if(c == 0){
        cout << "deleting a term" << endl;          
        delete start;
    }
}   
    cout << "Leaving set_coefficient()" << endl;
}

ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;  
for(unsigned int i = 0; start->next != 0; i++){
    o << "Term " << i << "'s coefficient is: " << start->coefficient << " degree is: " << start->degree << endl << flush;
    start = start->next;
}   
return o;
}

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top