地图和节点,错误 IntelliSense:不存在从 "const std::pair<const int, double>" 到 "Node" 的合适的用户定义转换

Map and Node, error IntelliSense: no suitable user-defined conversion from "const std::pair<const int, double>" to "Node" exists

我编写这段代码是为了使用地图和节点读取和添加多项式。

错误发生在operator+。我知道我没有将我的地图与 Node 相关联的代码,我想我应该在 "std::map PolynomialMap" 中使用一些东西;类似于列表,但我不确定是什么。 或者我应该完全改变我的代码并使用另一种方法? 如果问题不够好,请告诉我如何改进

#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();
}

问题是您已经编写了 AddOneTerm 方法以期望参数是 Node 结构,但是在 operator+ 方法中您试图向它传递一个参数那是 const std::pair<const int, double>.

有多种方法可以解决此问题。以下代码通过在节点 class 中提供可以将 const std::pair<const int, double> 转换为 Node 的构造函数来修改您的代码以解决问题。还需要添加无参数构造函数,因为当您通过读取文件构造多项式时,您正在使用 Node 的隐式无参数构造函数。

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