vendredi 2 décembre 2016

Redefinee the binary operator + with parametred class

I have my class Polynome but i have some porblem when i want to redefine the + operator.

#ifndef __POLYNOME_HPP__
#define __POLYNOME_HPP__

#include <iostream>
#include <map>
#include <set>
#include <math.h>
using namespace std;

template<class T>
class Polynome {

    private :
        map<int, T> _coef;
    public :
        Polynome<T>(): _coef() {

        }

        Polynome<T>(Polynome<T>& pol):_coef(pol._coef) {

        }

        Polynome<T>(const T arrayOfT[]) {
            for(int i = 0; i < sizeof(arrayOfT)/sizeof(arrayOfT[0]); i++) {
                _coef.insert(pair<int, T>(i, arrayOfT[i]));
            }
        }

        Polynome<T>(const set< pair<int, T> > s) {
            typename set< pair<int,T> >::iterator it;
            for(it = s.begin(); it!=s.end(); ++it) {
                _coef.insert(*it);
            }
        }

        friend ostream& operator<<(ostream& os, Polynome pol) {
            typename map<int,T>::iterator it;
            for (it=pol._coef.begin(); it!=pol._coef.end(); ++it) {
                if(it->first != 0) {
                    os << it->second << "x^" << it->first << " + ";
                }
            }
            os << " 0 " << endl;
            return os;
        }

        T evaluation(int x) {
            T result =0;

            typename map<int, T>::iterator it;

            for(it = _coef.begin(); it != _coef.end(); ++it) {


                result += pow(it->second, it->first) * x;

            }
            return result;
        }

        T& operator[](int indice) {
            return _coef[indice];
        }

        Polynome<T> derivatative() const {
            Polynome derivativePol;

            typename map<int, T>::iterator it;
            for(it = _coef.begin(); it != _coef.end(); ++it) {
                if(it->frist >= 1) {
                    pair<int, T> derivativePair = pair<int, T>(it->first - 1, it->first * it->second);
                    derivativePol.insert(derivativePair);
                }
            }

            return derivativePol;
        }

        friend Polynome<T> operator+(Polynome<T>& p, Polynome<T>& p2) {
            Polynome<T> pResult(p);
            typename map<int, T>::iterator it;
            for(it = p._coef.begin(); it != p._coef.end(); ++it) {

                pair<int, T> newPair = pair<int, T>(it->first, it->second + p2[it->first]);
                pResult._coef.insert(newPair);

            }

            return pResult;
        }
};
#endif

The compilator gives me the following error :

Polynome.cpp:32:19: error: no matching function for call to ‘Polynome::Polynome(Polynome)’

Polynome.cpp:32:19: note: candidates are: In file included from Polynome.cpp:1:0: Polynome.hpp:30:9: note: Polynome::Polynome(std::set >) [with T = int] Polynome(const set< pair > s) { ^ Polynome.hpp:30:9: note: no known conversion for argument 1 from ‘Polynome’ to ‘std::set, std::less >, std::allocator >

’ Polynome.hpp:24:9: note: Polynome::Polynome(const T*) [with T = int] Polynome(const T arrayOfT[]) { ^ Polynome.hpp:24:9: note: no known conversion for argument 1 from ‘Polynome’ to ‘const int*’ Polynome.hpp:20:9: note: Polynome::Polynome(Polynome&) [with T = int] Polynome(Polynome& pol):_coef(pol._coef) { ^ Polynome.hpp:20:9: note: no known conversion for argument 1 from ‘Polynome’ to ‘Polynome&’ Polynome.hpp:16:9: note: Polynome::Polynome() [with T = int] Polynome(): _coef() { ^ Polynome.hpp:16:9: note: candidate expects 0 arguments, 1 provided Polynome.hpp:37:25: error: initializing argument 2 of ‘std::ostream& operator<<(std::ostream&, Polynome)’ friend ostream& operator<<(ostream& os, Polynome pol) {

wich is related to this line in my main : cout << pol + pol2 << endl;

Where is the problem ?

Aucun commentaire:

Enregistrer un commentaire