-3

I wrote this code for reading and adding polynomials using map and nodes.

The error happens in operator+. I know I have no code which relates my map to Node, I think I should use something in "std::map PolynomialMap"; similar to list but I am not sure what. Or maybe I should totally change my codes and use another method? Please let me know how to improve my question if it's not good enough

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <vector>
#include <map>
using namespace std;

typedef struct Node
{
    double  cof;      // coefficient 
    int     deg;      // degree

} Node;               // the node of polynomial

class CPolynomial
{
private:
    std::map<int, double> PolynomialMap;

public:
    CPolynomial();
    CPolynomial(const string& file);
    virtual ~CPolynomial();

    CPolynomial operator+(const CPolynomial &right);
    CPolynomial& operator=(const CPolynomial &right);

private:
    void AddOneTerm(Node term);   // add one term into m_Polynomial 
};

int main()
{
    CPolynomial p1("P3.txt");
    CPolynomial p2("P4.txt");
    CPolynomial p3;

    p3 = p1 + p2;

    return 0;
}

CPolynomial::CPolynomial()
{
    ;
}

CPolynomial::CPolynomial(const string& file)
{
    Node term;
    fstream MyFile;
    string p;
    int num;

    MyFile.open(file);

    if (!MyFile.is_open())
    {
        cerr << "Unable to open input file" << endl;
        exit(EXIT_FAILURE);
    }
    else
    {
        MyFile >> p >> num;

        map <int, double>::iterator it = PolynomialMap.begin();

        for (int i = 0; i < num; i++)
        {
            MyFile >> term.deg >> term.cof;
            AddOneTerm(term);
        }
        MyFile.close();
    }
}



CPolynomial CPolynomial:: operator+(const CPolynomial &right)
{
    CPolynomial temp_polynomial;
    temp_polynomial.PolynomialMap = PolynomialMap;

    map <int, double> ::const_iterator it; it = right.PolynomialMap.begin();
    for (; it != right.PolynomialMap.end(); ++it) //
    {
        AddOneTerm(*it); //error C2664: 'void CPolynomial::AddOneTerm(Node)' : cannot convert argument 1 from 'const std::pair<const _Kty,_Ty>' to 'Node'
                          //IntelliSense: no suitable user-defined conversion from "const std::pair<const int, double>" to "Node" exists
    }
    map <int, double> sum_result = PolynomialMap;
    PolynomialMap = temp_polynomial.PolynomialMap;
    temp_polynomial.PolynomialMap = sum_result;
    sum_result.clear();

    return temp_polynomial;
}

CPolynomial& CPolynomial:: operator=(const CPolynomial &right)
{

    this->PolynomialMap = right.PolynomialMap;
    return *this;
}

void CPolynomial::AddOneTerm(Node term)
{
    auto it = PolynomialMap.begin();
    while (it != PolynomialMap.end() && it->first < term.deg)
    {
        ++it;
    }

    if (it != PolynomialMap.end() && term.deg == it->first)
    {
        it->second += term.cof;
    }
    else
    {
        PolynomialMap.insert(pair<int, double>(term.deg, term.cof));
    }
}

CPolynomial::~CPolynomial()
{
    PolynomialMap.clear();
}
Saeedeh
  • 7
  • 6

1 Answers1

0

The problem is that you have written your AddOneTerm method to expect an argument that is a Node struct, but in the operator+ method you are attempting to pass it an argument that is a const std::pair<const int, double>.

There are a variety of ways to fix the problem. The following code modifies your code to resolve the problem by providing a constructor in the Node class that can convert a const std::pair<const int, double> into a Node. The addition of a no-argument constructor is also required, since you are using the implicit no-argument constructor of Node when you construct a polynomial by reading a file.

typedef struct Node
{
    double  cof;      // coefficient
    int     deg;      // degree

    Node()
    {
         deg = 0;
         cof = 0.0;
    }

    Node(const pair<const int, double> & nodePair)
    {
        deg = nodePair.first;
        cof = nodePair.second;
    }
} Node;               // the node of polynomial
Evan VanderZee
  • 797
  • 6
  • 10
  • Thank you Evan, that was a simple and useful solution. Can you please let me know what other method would you use for this problem? – Saeedeh Jul 24 '16 at 12:56
  • Two other ways to fix the problem are (1) add another method to `CPolynomial` with the method signature `void AddOneTerm(const pair)` that creates a `Node` and calls the existing `AddOneTerm` method and (2) change the `AddOneTerm` method to have an `int` argument and a `double` argument instead of a `Node` argument, unpack the pair to `it->first` and `it-second` when calling the `AddOneTerm` method, and work directly with `deg` and `cof` variables in the `CPolynomial` constructor. With approach (2) the `Node` struct could be dropped. – Evan VanderZee Jul 24 '16 at 17:10